[我正在尝试用另一个框架创建Python 3.7+ Tkinter应用,可以通过单击按钮将其调出:
我使用Tkinter(ThemedTk)创建一个类,并希望使用ttk.Frame创建几个类。按钮将在框架之间切换。我现在的问题-我无法调整框架的大小并将“ nesw”粘贴到主根类。我以为我在ttk.Frame上做错了什么,但我不明白这个问题。我添加了
self.grid(row=0, column=0)
self.rowconfigure(0, weight=3)
self.columnconfigure(0, weight=3)
但似乎没有帮助...整个代码:
import tkinter as tk
from tkinter import ttk
from ttkthemes import ThemedTk
class MainApp(ThemedTk):
def __init__(self):
ThemedTk.__init__(self)
self.set_theme('equilux', themebg=True)
self.title('Test App')
self.style = ttk.Style(self)
self.DESKTOP_WIDTH = self.winfo_screenwidth()
self.DESKTOP_HEIGHT = self.winfo_screenheight()
self.WIDTH = int(self.DESKTOP_WIDTH / 4)
self.HEIGHT = int(self.DESKTOP_HEIGHT / 2.5)
self.geometry(f'{self.WIDTH}x{self.HEIGHT}+{int(self.DESKTOP_WIDTH / 3)}+{int(self.DESKTOP_HEIGHT / 4)}')
self.minsize(width=self.WIDTH, height=self.HEIGHT)
self._frame = None
self.switch_frame(MainLogin)
def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.grid(row=0, column=0)
class MainLogin(ttk.Frame):
def __init__(self, master):
ttk.Frame.__init__(self, master, padding=(5, 5, 5, 5), borderwidth=10, relief='ridge', sticky='nesw')
self.grid(row=0, column=0)
self.rowconfigure(0, weight=3)
self.columnconfigure(0, weight=3)
self.master = master
self.userid = tk.StringVar()
self.password = tk.StringVar()
self.remember = tk.StringVar()
self.frame_login = ttk.Frame(self, padding=(10, 10, 10, 10), borderwidth=2, relief='ridge')
self.label_welcome = ttk.Label(self.frame_login, text='Open App')
self.label_user = ttk.Label(self.frame_login, text='User ID:')
self.entry_user = ttk.Entry(self.frame_login, textvariable=self.userid)
self.label_password = ttk.Label(self.frame_login, text='Password:')
self.entry_password = ttk.Entry(self.frame_login, textvariable=self.password, show='*')
self.check_remember = ttk.Checkbutton(self.frame_login, text='Remember me', variable=self.remember, onvalue='yes', offvalue='no')
self.button_login = ttk.Button(self.frame_login, text='Login', command=lambda: MainLogin.login(self.userid, self.password))
self.button_register = ttk.Button(self.frame_login, text='Register', command=self.register)
self.grid_widgets()
self.resize_widgets()
def grid_widgets(self):
self.frame_login.grid(row=0, column=0, sticky='nesw')
self.label_welcome.grid(row=0, column=0, columnspan=2, pady=30, sticky='n')
self.label_user.grid(row=1, column=0, padx=10, pady=5, sticky='e')
self.entry_user.grid(row=1, column=1, padx=10, pady=5, sticky='w')
self.label_password.grid(row=2, column=0, padx=10, pady=5, sticky='e')
self.entry_password.grid(row=2, column=1, padx=10, pady=5, sticky='w')
self.check_remember.grid(row=3, column=0, columnspan=2, pady=5, sticky='n')
self.button_login.grid(row=4, column=0, padx=10, pady=5, ipadx=30, sticky='e')
self.button_register.grid(row=4, column=1, padx=10, pady=5, ipadx=30, sticky='w')
def resize_widgets(self):
self.columnconfigure(0, weight=3)
self.rowconfigure(0, weight=3)
self.frame_login.columnconfigure(0, weight=3)
self.frame_login.columnconfigure(1, weight=3)
@staticmethod
def login(userif, password):
print('Login button pressed')
def register(self):
print('Register button pressed')
if __name__ == '__main__':
root = MainApp()
root.mainloop()
pack
而不是grid
,因为您不必担心配置行和列。def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack(fill="both", expand=True)