Python 多个窗口的 Tkinter 示例代码,为什么按钮不能正确加载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16115378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tkinter example code for multiple windows, why won't buttons load correctly?
提问by ADB
I am writing a program which should:
我正在编写一个程序,它应该:
- Open a window with the press of a button.
- Close the newly opened window with the press of another button.
- 按下按钮打开一个窗口。
- 按下另一个按钮关闭新打开的窗口。
I'm using classes so I can insert the code into a larger program later. However, I can't get my buttons to load correctly.
我正在使用类,以便以后可以将代码插入到更大的程序中。但是,我无法让我的按钮正确加载。
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()
采纳答案by Rushy Panchal
I rewrote your code in a more organized, better-practiced way:
我以更有条理、更实用的方式重写了您的代码:
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()
Result:
结果:
回答by Jared
You need to specify the master for the second button. Otherwise it will get packed onto the first window. This is needed not only for Button, but also for other widgets and non-gui objects such as StringVar.
您需要为第二个按钮指定母版。否则它会被打包到第一个窗口。这不仅对于Button,而且对于其他小部件和非 gui 对象(例如StringVar.
Quick fix:add the frame newas the first argument to your Buttonin Demo2.
快速修复:将框架new作为第一个参数添加到您的Buttonin Demo2.
Possibly better:Currently you have Demo2inheriting from tk.Framebut I think this makes more sense if you change Demo2to be something like this,
可能更好:目前您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()
Just as a suggestion, you should only import tkinteronce. Pick one of your first two import statements.
作为一个建议,您应该只导入tkinter一次。选择前两个导入语句之一。
回答by Sumanta
#!/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()
回答by WhyAreYouReadingThis
What you could do is copy the code from tkinter.pyinto a file called mytkinter.py, then do this code:
您可以做的是将代码复制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)
And you have two windows which don't collide!
而且你有两个不会碰撞的窗口!
回答by Giovanni G. PY
I tried to use more than two windows using the Rushy Panchal example above. The intent was to have the change to call more windows with different widgets in them. The butnew function creates different buttons to open different windows. You pass as argument the name of the class containing the window (the second argument is nt necessary, I put it there just to test a possible use. It could be interesting to inherit from another window the widgets in common.
我尝试使用上面的 Rushy Panchal 示例使用两个以上的窗口。目的是进行更改以调用更多带有不同小部件的窗口。butnew 函数创建不同的按钮来打开不同的窗口。您将包含窗口的类的名称作为参数传递(第二个参数不是必需的,我把它放在那里只是为了测试可能的用途。从另一个窗口继承共同的小部件可能会很有趣。
import tkinter as tk
class Demo1:
def __init__(self, master):
self.master = master
self.master.geometry("400x400")
self.frame = tk.Frame(self.master)
self.butnew("Window 1", "ONE", Demo2)
self.butnew("Window 2", "TWO", Demo3)
self.frame.pack()
def butnew(self, text, number, _class):
tk.Button(self.frame, text = text, width = 25, command = lambda: self.new_window(number, _class)).pack()
def new_window(self, number, _class):
self.newWindow = tk.Toplevel(self.master)
_class(self.newWindow, number)
class Demo2:
def __init__(self, master, number):
self.master = master
self.master.geometry("400x400+400+400")
self.frame = tk.Frame(self.master)
self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
self.label = tk.Label(master, text=f"this is window number {number}")
self.label.pack()
self.quitButton.pack()
self.frame.pack()
def close_windows(self):
self.master.destroy()
class Demo3:
def __init__(self, master, number):
self.master = master
self.master.geometry("400x400+400+400")
self.frame = tk.Frame(self.master)
self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
self.label = tk.Label(master, text=f"this is window number {number}")
self.label.pack()
self.label2 = tk.Label(master, text="THIS IS HERE TO DIFFERENTIATE THIS WINDOW")
self.label2.pack()
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()
Open the new window only once
只打开一次新窗口
To avoid having the chance to press multiple times the button having multiple windows... that are the same window, I made this script (take a look at this pagetoo)
为了避免有机会多次按下具有多个窗口的按钮......这是同一个窗口,我制作了这个脚本(也看看这个页面)
import tkinter as tk
def new_window1():
global win1
try:
if win1.state() == "normal": win1.focus()
except:
win1 = tk.Toplevel()
win1.geometry("300x300+500+200")
win1["bg"] = "navy"
lb = tk.Label(win1, text="Hello")
lb.pack()
win = tk.Tk()
win.geometry("200x200+200+100")
button = tk.Button(win, text="Open new Window")
button['command'] = new_window1
button.pack()
win.mainloop()


