我写一个程序,它应该:
- 用按下按钮打开一个窗口。
- 与其他按钮的按下关闭新打开的窗口。
我使用的是类,所以我可以在以后将代码插入到一个更大的计划。 但是,我不能让我的按钮正确加载。
import tkinter as tk
class Demo1(tk.Frame):
def __init__(self):
tk.Frame.__init__(self)
self.pack()
self.master.title("Demo 1")
self.button1 = tk.Button(self, text = "Button 1", width = 25,
command = self.new_window)
self.button1.grid(row = 0, column = 1, columnspan = 2, sticky = tk.W+tk.E+tk.N+tk.S)
def new_window(self):
self.newWindow = Demo2()
class Demo2(tk.Frame):
def __init__(self):
new = tk.Frame.__init__(self)
new = tk.Toplevel(self)
new.title("Demo 2")
new.button = tk.Button(text = "Button 2", width = 25,
command = self.close_window)
new.button.pack()
def close_window(self):
self.destroy()
def main():
Demo1().mainloop()
if __name__ == '__main__':
main()
我改写了你的代码在一个更有条理,好熟练的方式:
import tkinter as tk
class Demo1:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.button1 = tk.Button(self.frame, text = 'New Window', width = 25, command = self.new_window)
self.button1.pack()
self.frame.pack()
def new_window(self):
self.newWindow = tk.Toplevel(self.master)
self.app = Demo2(self.newWindow)
class Demo2:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
self.quitButton.pack()
self.frame.pack()
def close_windows(self):
self.master.destroy()
def main():
root = tk.Tk()
app = Demo1(root)
root.mainloop()
if __name__ == '__main__':
main()
结果:
你需要指定主为第二个按钮。 否则,它会被打包到的第一个窗口。 这不仅需要对Button
,同时也为其他部件和非GUI对象,如StringVar
。
快速修复:框架添加new
的第一个参数,以您的Button
在Demo2
。
可能更好:目前你有Demo2
继承tk.Frame
,但我认为,如果你改变了这个更有意义Demo2
是这样的事情,
class Demo2(tk.Toplevel):
def __init__(self):
tk.Toplevel.__init__(self)
self.title("Demo 2")
self.button = tk.Button(self, text="Button 2", # specified self as master
width=25, command=self.close_window)
self.button.pack()
def close_window(self):
self.destroy()
正如一个建议,你应该只导入tkinter
一次。 选择你的前两个导入语句之一。
#!/usr/bin/env python
import Tkinter as tk
from Tkinter import *
class windowclass():
def __init__(self,master):
self.master = master
self.frame = tk.Frame(master)
self.lbl = Label(master , text = "Label")
self.lbl.pack()
self.btn = Button(master , text = "Button" , command = self.command )
self.btn.pack()
self.frame.pack()
def command(self):
print 'Button is pressed!'
self.newWindow = tk.Toplevel(self.master)
self.app = windowclass1(self.newWindow)
class windowclass1():
def __init__(self , master):
self.master = master
self.frame = tk.Frame(master)
master.title("a")
self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25 , command = self.close_window)
self.quitButton.pack()
self.frame.pack()
def close_window(self):
self.master.destroy()
root = Tk()
root.title("window")
root.geometry("350x50")
cls = windowclass(root)
root.mainloop()
你可以做的是将代码从复制tkinter.py
到一个文件名为mytkinter.py
,然后执行以下代码:
import tkinter, mytkinter
root = tkinter.Tk()
window = mytkinter.Tk()
button = mytkinter.Button(window, text="Search", width = 7,
command=cmd)
button2 = tkinter.Button(root, text="Search", width = 7,
command=cmdtwo)
你有两个窗口不发生碰撞!
文章来源: Tkinter example code for multiple windows, why won't buttons load correctly?