Python 为什么所有内容都显示在主窗口中而不是特定的框架中?
所以我开始创建GUI,我将使用它作为电动引擎的测试台。我的问题是为什么主窗口中的所有内容都是这样显示的 而不是两帧 我第一次写它时,它工作得非常完美: [ 但是后来我想用类重新排列代码,问题出现了,我使用的是Python3.7 我的代码:Python 为什么所有内容都显示在主窗口中而不是特定的框架中?,python,python-3.x,user-interface,tkinter,Python,Python 3.x,User Interface,Tkinter,所以我开始创建GUI,我将使用它作为电动引擎的测试台。我的问题是为什么主窗口中的所有内容都是这样显示的 而不是两帧 我第一次写它时,它工作得非常完美: [ 但是后来我想用类重新排列代码,问题出现了,我使用的是Python3.7 我的代码: import tkinter as tk from tkinter import * from tkinter import messagebox class Lintebench(): def __init__(self, master):
import tkinter as tk
from tkinter import *
from tkinter import messagebox
class Lintebench():
def __init__(self, master):
self.master = master
self.master.title("Linte^2 testbench")
self.master.geometry('1000x600')
#FRAMES
frame = Frame(master, bg='#3e646c').place(relwidth=0.2, relheight=0.4, rely=0.6)
frame2 = Frame(master, bg='#3e646c').place(relwidth=0.8, relheight=0.4, rely=0.6, relx=0.2)
#FRAME 1
self.start_button = Button(frame, text="Start", padx=50, pady=50, bg='green', activebackground='grey', command=self.start_engine).pack()
self.stop_button = Button(frame2, text="Stop", padx=50, pady=50, bg='red', activebackground='grey', command=self.stop_engine).pack()
#FRAME 2
self.parameters = Label(frame2, text="PARAMETERS", font=("Arial", 16), fg='white', bg='#3e646c').place(relx=0)
self.set_parameter = Label(frame2, text="SET", font=("Arial", 16), fg='white', bg='#3e646c').place(relx=0.8)
self.values = Label(frame2, text="VALUES", font=("Arial", 16), fg='white', bg='#3e646c').place(relx=0.4)
self.torque = Label(frame2, text="TORQUE", font=("Arial", 12), fg='white', bg='#3e646c').place(relx=0.7, rely=0.2)
self.velocity = Label(frame2, text="VELOCITY", font=("Arial", 12), fg='white', bg='#3e646c').place(relx=0.7, rely=0.6)
#parameters to set
self.set_torque = Scale(frame2, orient=HORIZONTAL, length=200).place(relx=0.7, rely=0.3)
self.set_velocity = Scale(frame2, orient=HORIZONTAL, length=200).place(relx=0.7, rely=0.7)
def start_engine(*args):
messagebox.showinfo('Information','Engine was started')
def stop_engine(*args):
messagebox.showinfo('Information','Engine was stopped')
def main():
root = Tk()
lintebench = Lintebench(root)
root.mainloop()
if __name__ == '__main__':
main()here
我相信这是因为你把他们的主人当作根,你只有一个根,当你做
roo.mainloop()
时,他们就会集中注意力。这是因为你把Frame(…)
和place(…)
因此frame
和frame2
将是None
。将链接语句分为两个语句:
frame = Frame(master, bg='#3e646c')
frame.place(relwidth=0.2, relheight=0.4, rely=0.6)
frame2 = Frame(master, bg='#3e646c')
frame2.place(relwidth=0.8, relheight=0.4, rely=0.6, relx=0.2)
实际上,如果您想在其他地方引用小部件,您需要分离代码中的所有链接语句
另外,self.stop_按钮
应该是frame
的子项,而不是frame2
:
self.stop_button = Button(frame, text="Stop", padx=50, pady=50, bg='red', activebackground='grey', command=self.stop_engine)
self.stop_button.pack()
请在问题中以文本形式提供您的代码。不要使用屏幕截图或指向托管您代码的其他网站的链接。您是否查看了帧变量的值,以查看它们是否与您所认为的相同?请仔细阅读,因为
frame
和frame2
都是无的,因为它们被分配了place(…)的结果
,而不是Frame(…)
。您已将Frame(…)
链接到place(…)
。请将它们分成两条语句。