Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ttk tkinter多帧/窗口_Python_User Interface_Python 3.x_Tkinter_Ttk - Fatal编程技术网

Python ttk tkinter多帧/窗口

Python ttk tkinter多帧/窗口,python,user-interface,python-3.x,tkinter,ttk,Python,User Interface,Python 3.x,Tkinter,Ttk,我创建的以下应用程序用于演示tkinter中的多个窗口。主要问题是,无论是bmi计算器还是转换器中的条目控件都不接受条目框中的值-当我进行计算时,它们会得到值错误 from tkinter import * from tkinter import ttk from tkinter import messagebox class App1(ttk.Frame): def createWidgets(self): #text variables self.i

我创建的以下应用程序用于演示tkinter中的多个窗口。主要问题是,无论是bmi计算器还是转换器中的
条目
控件都不接受条目框中的值-当我进行计算时,它们会得到
值错误

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() 从tkinter进口* 从tkinter导入ttk 从tkinter导入消息框 类App1(ttk.Frame): def createWidgets(自): #文本变量 self.i_height=StringVar() self.i_weight=StringVar() self.o_bmi=StringVar() #标签 self.label1=ttk.Label(self,text=“输入您的权重:”).grid(行=0,列=0,粘性=W) self.label2=ttk.Label(self,text=“输入您的高度:”).grid(行=1,列=0,粘性=W) self.label3=ttk.Label(self,text=“你的体重指数是:”).grid(行=2,列=0,粘性=W) #文本框 self.textbox1=ttk.Entry(self,textvariable=self.i_weight).grid(行=0,列=1,粘滞=E) self.textbox2=ttk.Entry(self,textvariable=self.i_height).grid(行=1,列=1,粘滞=E) self.textbox3=ttk.Entry(self,textvariable=self.o_bmi).grid(行=2,列=1,粘性=E) #钮扣 self.button1=ttk.Button(self,text=“取消/退出”,command=self.Quit).grid(行=3,列=1,粘性=E) self.button1=ttk.Button(self,text=“Ok”,command=self.calculateBmi).grid(行=3,列=2,粘滞=E) def calculateBmi(自身): 尝试: self.weight=float(self.i\u weight.get()) self.height=float(self.i\u height.get()) self.bmi=体重/身高**2.0 self.o_bmi.set(self.bmi) 除值错误外: showinfo(“错误”,“您只能使用数字”) 最后: 自身重量设置(“”) self.i_height.set(“”) def uuu init uuu(self,master=None): ttk.Frame.\uuuuu初始化(自,主) self.grid() self.createWidgets() 类App2(ttk.Frame): def创建_小部件(自): “”“为GUI创建小部件”“” #1个文本框(stringvar) self.entry=StringVar() self.textBox1=ttk.Entry(self,textvariable=self.Entry).grid(行=0,列=1) #5个标签(3个静态标签,1个stringvar标签) self.displayLabel1=ttk.Label(self,text=“feets”).grid(行=0,列=2,粘性=W) self.displayLabel2=ttk.Label(self,text=“相当于:”).grid(行=1,列=0) self.result=StringVar() self.displayLabel3=ttk.Label(self,textvariable=self.result).grid(行=1,列=1) self.displayLabel4=ttk.Label(self,text=“meters”).grid(行=1,列=2,粘性=W) #2个按钮 self.quitButton=ttk.Button(self,text=“Quit”,command=self.Quit).grid(行=2,列=1,粘性=(S,E)) self.calculateButton=ttk.Button(self,text=“Calculate”,command=self.convert_feet_to_meters)。网格(行=2,列=2,粘性=(S,E)) def将英尺转换为米(自身): “”“将英尺转换为米,使用字符串变量并将其转换为浮点数”“” self.measurement=float(self.entry.get()) self.meters=self.measurement*0.3048 self.result.set(self.meters) def uuu init uuu(self,master=None): ttk.Frame.\uuuuu初始化(自,主) self.grid() self.create_widgets() def按钮1\u单击() root=Tk() app=App1(master=root) app.mainloop() def按钮2\u单击() root=Tk() app=App2(master=root) app.mainloop() def main(): window=Tk() button1=ttk.Button(窗口,text=“bmi calc”,命令=button1\u单击)。网格(行=0,列=1) button2=ttk.Button(窗口,text=“feet conv”,命令=button2\u click)。网格(行=1,列=1) window.mainloop() 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': main()
如何修复这个问题,但仍然保持类结构和python3的使用?顺便说一句-任何类似于C#的
form1.Show()

您必须将
StringVar()
转换为整数/浮点或使用
IntVar()
DoubleVar()

还有其他问题。以下语句返回“None”,因为它是
grid()
对象,而不是
Label()
对象:

self.label1 = ttk.Label(self, text="Enter your weight:").grid(row=0, column=0, sticky=W)   

我想如果同时打开两个
Tk()
窗口,您也会遇到问题。使用
Toplevel()
或单独的框架来代替。

谢谢Joe,你帮了我不少忙,我终于找到了答案。。。使用ttk可以使用笔记本,因为我的两个类都是派生框架,所以我可以将它们添加到笔记本的框架中

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()

这就解决了问题:)

网格不是对象,也不返回对象。这只是一个函数。但是你是对的,Label(…).grid(…)并没有返回任何有用的东西。它只用于布局,是在函数create_widgets中设置框架中的小部件。我使用它作为一个类,因为它有助于在一个地方设置所有小部件,并且我喜欢使用单行程序:)@py_tk_coder:如果是这种情况,请不要将结果分配给实例变量。它是尖头的、令人困惑的,并且使您的“单行线”超出了必要的长度。