Tkinter按钮不调用命令功能

时间:2019-11-15 01:08:12

标签: python python-3.x tkinter

这似乎应该是一个简单的问题,但是到目前为止,我在网上找不到的任何方法都不能解决它-如果此问题很明显,我深表歉意! 我试图在单击按钮时更新结果标签,但是代码无法运行,并给出错误消息:与button命令绑定的函数不存在(即使在它旁边声明了该功能)。我在这里想念什么? 代码:

import tkinter as tk
from tkinter import ttk
from tkinter import *

#This confirms that a given character is a digit or a decimal point
#
def numeric_input_validation(character):
    if character.isdigit() or character == "" or character == ".":
        return True
    return False

class PanelManager(tk.Tk):
    def __init__ (self):
        tk.Tk.__init__(self)

        #Titling Window, setting up panel manager
        self.title("Combat Robotics Calculator")
        self.panel_manager = ttk.Notebook(self)

        #Sizing and Scaling specs
        self.minsize(425,100)
        self.columnconfigure(0, weight = 1)
        self.rowconfigure(0, weight = 1)

        #Adding tabs
        self.add_battery_tab("Batteries")
        self.add_tab("Pulleys")
        self.add_tab("Drive System")
        self.add_tab("Weapon System")

    def add_tab(self, title):
        tab_frame = Frame(self.panel_manager)
        self.panel_manager.add(tab_frame, text = title)
        self.panel_manager.grid()

    def add_battery_tab(self, title):
        battery_tab = BatteryTab(self)
        self.panel_manager.add(battery_tab, text = title)
        self.panel_manager.grid(sticky = "news")

    #Infinite Loop
    def run(self):
        self.mainloop()


class BatteryTab(tk.Frame):
    def __init__ (self, master):
        tk.Frame.__init__(self, master)
        #Scaling Configurations
        self.columnconfigure(0, weight = 1)
        self.rowconfigure(0, weight = 1)
        self.columnconfigure(1, weight = 1)

        #Capacity Calculator
        capacity_calcf = BatteryCapacityLFrame(self)
        capacity_calcf.grid(row = 0, column = 0, sticky = "news", padx = 5, pady = 5)

        #C Rating Calculator
        crating_calcf = CRatingRFrame(self)
        crating_calcf.grid(row = 0, column = 1, sticky = "news", padx = 5, pady = 5)

class BatteryCapacityLFrame(tk.LabelFrame):
    def __init__ (self, master):
        tk.LabelFrame.__init__(self, master)
        self.config(text = "Battery Capacity Calculator")

        #Info Labels
        tk.Label(self, text = "Time Required (minutes)").grid(row = 0, column = 0, sticky = "news", padx = 5, pady = 5)
        tk.Label(self, text = "Peak Current (Amps)").grid(row = 1, column = 0, sticky = "news", padx = 5, pady = 5)
        tk.Label(self, text = "Avg Current Draw (%)").grid(row = 2, column = 0, sticky = "news", padx = 5, pady = 5)

        #Input Boxes
        time_required_entry = tk.Entry(self)
        peak_current_entry = tk.Entry(self)
        avg_current_draw_entry = tk.Entry(self)
        input_list = [time_required_entry, peak_current_entry, avg_current_draw_entry]
        #Assigning grid locations for each input box
        for i in range(len(input_list)):
            input_list[i].grid(row = i, column = 1, sticky = "news", padx = 5, pady = 5)

        #TCL to Python callback
        callback = self.register(numeric_input_validation)
        for entry in input_list:
            entry.configure(validate = "key", validatecommand = (callback, '%S'))

###THIS IS THE BUTTON I'M TRYING TO USE TO CALL THE FUNCTION###
        #Button
        calc_battery_capacity_button = tk.Button(self, text = "Calculate", command = change_results_label)
        calc_battery_capacity_button.grid(row = 3, column = 0, sticky = "news", padx = 5, pady = 5)
        #Result Label
        tk.Label(self, text = "Result: 0 mAh").grid(row = 3, column = 1, sticky = "news", padx = 5, pady = 5)

    #Calculates required battery capacity to run at a given percent of peak current
    #for a given amount of time in minutes
    def calc_battery_capacity(time, peak_current, percent_draw):
        #time in minutes, peak_current in Amps, percent_draw in percentage
        if(time and peak_current and percent_draw):
            required_capacity = ((float(time)/60) * float(peak_current) * (float(percent_draw)/100) * 1000)
            return int(required_capacity)
        return -1
###THIS IS THE FUNCTION I'M TRYING TO CALL###
    def change_results_label():
        results = calc_battery_capacity(time_required_entry, peak_current_entry, avg_current_draw_entry)
        if not results == -1:
            self.calc_battery_capacity_button.config(text = "Results: " + results + " mAh")
        else:
            self.calc_battery_capacity_button.config(text = "Please fill out all relevant fields!")

root_window = PanelManager()
root_window.run()

这是我得到的错误:

Traceback (most recent call last):
  File "CBCalcDriver.py", line 116, in <module>
    root_window = PanelManager()
  File "CBCalcDriver.py", line 26, in __init__
    self.add_battery_tab("Batteries")
  File "CBCalcDriver.py", line 37, in add_battery_tab
    battery_tab = BatteryTab(self)
  File "CBCalcDriver.py", line 55, in __init__
    capacity_calcf = BatteryCapacityLFrame(self)
  File "CBCalcDriver.py", line 88, in __init__
    calc_battery_capacity_button = tk.Button(self, text = "Calculate", command = change_results_label)
NameError: name 'change_results_label' is not defined

非常感谢您的宝贵时间!我是tkinter和Python的新手,因此非常感谢您提出有关改进代码的建议!

0 个答案:

没有答案