我对 tkinter 真的很陌生,我想尝试在 tkinter 中使用多个窗口。我从这里借用了代码:https ://pythonprogramming.altervista.org/create-more-windows-with-tkinter/?doing_wp_cron=1645372524.8916330337524414062500 我希望能够将按钮从刚刚居中移动到中间在窗口的顶部,但我不确定如何移动窗口中的按钮。我知道如何使用 pack() 通常将按钮移动到类之外,但我不确定如何在类和此代码中执行此操作。当我尝试添加边或填充等属性时,会出现窗口,但不会出现按钮。我到处寻找解决方案,但没有找到适合我的问题的任何东西
代码:
import tkinter as tk
class Win1:
def __init__(self, master):
self.master = master
self.master.geometry("800x800")
self.frame = tk.Frame(self.master)
self.butnew("Click to open Window 2", "2", Win2)
self.butnew("Click to open Window 3", "3", Win3)
self.frame.pack()
def butnew(self, text, number, _class):
tk.Button(self.frame, text = text, command= lambda: self.new_window(number, _class)).pack()
def new_window(self, number, _class):
self.new = tk.Toplevel(self.master)
_class(self.new, number)
class Win2:
def __init__(self, master, number):
self.master = master
self.master.geometry("800x800+200+200")
self.frame = tk.Frame(self.master)
self.quit = tk.Button(self.frame, text = f"Quit this window n. {number}", command = self.close_window)
self.quit.pack()
self.frame.pack()
def close_window(self):
self.master.destroy()
class Win3:
def __init__(self, master, number):
self.master = master
self.master.geometry("800x800+200+200")
self.frame = tk.Frame(self.master)
self.quit = tk.Button(self.frame, text = f"Quit this window n. {number}", command = self.close_window)
self.quit.pack()
self.label = tk.Label(self.frame, text="THIS IS ONLY IN THE THIRD WINDOW")
self.label.pack()
self.frame.pack()
def close_window(self):
self.master.destroy()
root = tk.Tk()
app = Win1(root)
root.mainloop()