ttk tkinter multiple frames/windows
我创建的以下应用程序用于演示Tkinter中的多个窗口。主要的问题是,无论是在bmi计算器还是转换器中,没有一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | from tkinter import * from tkinter import ttk from tkinter import messagebox class App1(ttk.Frame): def createWidgets(self): #text variables self.i_height = StringVar() self.i_weight = StringVar() self.o_bmi = StringVar() #labels self.label1 = ttk.Label(self, text="Enter your weight:").grid(row=0, column=0, sticky=W) self.label2 = ttk.Label(self, text="Enter your height:").grid(row=1, column=0, sticky=W) self.label3 = ttk.Label(self, text="Your BMI is:").grid(row=2, column=0, sticky=W) #text boxes self.textbox1 = ttk.Entry(self, textvariable=self.i_weight).grid(row=0, column=1, sticky=E) self.textbox2 = ttk.Entry(self, textvariable=self.i_height).grid(row=1, column=1, sticky=E) self.textbox3 = ttk.Entry(self, textvariable=self.o_bmi).grid(row=2, column=1, sticky=E) #buttons self.button1 = ttk.Button(self, text="Cancel/Quit", command=self.quit).grid(row=3, column=1, sticky=E) self.button1 = ttk.Button(self, text="Ok", command=self.calculateBmi).grid(row=3, column=2, sticky=E) def calculateBmi(self): try: self.weight = float(self.i_weight.get()) self.height = float(self.i_height.get()) self.bmi = self.weight / self.height ** 2.0 self.o_bmi.set(self.bmi) except ValueError: messagebox.showinfo("Error","You can only use numbers.") finally: self.i_weight.set("") self.i_height.set("") def __init__(self, master=None): ttk.Frame.__init__(self, master) self.grid() self.createWidgets() class App2(ttk.Frame): def create_widgets(self): """Create the widgets for the GUI""" #1 textbox (stringvar) self.entry= StringVar() self.textBox1= ttk.Entry(self, textvariable=self.entry).grid(row=0, column=1) #5 labels (3 static, 1 stringvar) self.displayLabel1 = ttk.Label(self, text="feet").grid(row=0, column=2, sticky=W) self.displayLabel2 = ttk.Label(self, text="is equivalent to:").grid(row=1, column=0) self.result= StringVar() self.displayLabel3 = ttk.Label(self, textvariable=self.result).grid(row=1, column=1) self.displayLabel4 = ttk.Label(self, text="meters").grid(row=1, column=2, sticky=W) #2 buttons self.quitButton = ttk.Button(self, text="Quit", command=self.quit).grid(row=2, column=1, sticky=(S,E)) self.calculateButton = ttk.Button(self, text="Calculate", command=self.convert_feet_to_meters).grid(row=2, column=2, sticky=(S,E)) def convert_feet_to_meters(self): """Converts feet to meters, uses string vars and converts them to floats""" self.measurement = float(self.entry.get()) self.meters = self.measurement * 0.3048 self.result.set(self.meters) def __init__(self, master=None): ttk.Frame.__init__(self, master) self.grid() self.create_widgets() def button1_click(): root = Tk() app = App1(master=root) app.mainloop() def button2_click(): root = Tk() app = App2(master=root) app.mainloop() def main(): window = Tk() button1 = ttk.Button(window, text="bmi calc", command=button1_click).grid(row=0, column=1) button2 = ttk.Button(window, text="feet conv", command=button2_click).grid(row=1, column=1) window.mainloop() if __name__ == '__main__': main() |
如何修复这个问题,但仍然保持类结构和python3的使用?顺便问一句-有什么类似于C的
您必须将stringvar()转换为整数/浮点,或者使用intvar()或doublevar()。
还有其他问题。像下面这样的语句返回"none",因为它是一个grid()对象,而不是一个label()对象:
1 | self.label1 = ttk.Label(self, text="Enter your weight:").grid(row=0, column=0, sticky=W) |
我认为同时打开两个tk()窗口也会有问题。使用toplevel()或单独的帧。
多谢乔,你帮了我不少忙,我无意中找到了答案…使用TTK你可以使用笔记本,因为我的两个类都是派生的框架,我可以将它们添加到笔记本的框架中…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | def main(): #Setup Tk() window = Tk() #Setup the notebook (tabs) notebook = ttk.Notebook(window) frame1 = ttk.Frame(notebook) frame2 = ttk.Frame(notebook) notebook.add(frame1, text="BMI Calc") notebook.add(frame2, text="Feet to Meters") notebook.grid() #Create tab frames app1 = App1(master=frame1) app1.grid() app2 = App2(master=frame2) app2.grid() #Main loop window.mainloop() |
这就解决了问题。)