class LoginFrame(tk.Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.master.title('登录系统') self.master.geometry('300x200') self.username_label = tk.Label(self.master, text='用户名:') self.username_label.pack() self.username_entry = tk.Entry(self.master) self.username_entry.pack() self.password_label = tk.Label(self.master, text='密码:') self.password_label.pack() self.password_entry = tk.Entry(self.master, show='*') self.password_entry.pack() self.login_button = tk.Button(self.master, text='登录', command=self.login) self.login_button.pack() def login(self): username = self.username_entry.get() password = self.password_entry.get() if username == 'AAA' and password == '111': self.master.destroy() root = tk.Tk() app = MainFrame(root) app.mainloop() else: tk.messagebox.showerror('错误', '用户名或密码错误!')修改上述代码,使得text用户和密码和输入栏分别处于一行
时间: 2023-06-25 20:08:20 浏览: 178
Programming--management-system.zip_java programming_词典 统计显示
可以将Label和Entry分别放在同一行,可以使用pack或grid方法实现。以下是使用pack实现的示例代码,将Label和Entry放在同一行:
```
class LoginFrame(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title('登录系统')
self.master.geometry('300x200')
self.username_frame = tk.Frame(self.master)
self.username_frame.pack()
self.username_label = tk.Label(self.username_frame, text='用户名:')
self.username_label.pack(side=tk.LEFT)
self.username_entry = tk.Entry(self.username_frame)
self.username_entry.pack(side=tk.LEFT)
self.password_frame = tk.Frame(self.master)
self.password_frame.pack()
self.password_label = tk.Label(self.password_frame, text='密码:')
self.password_label.pack(side=tk.LEFT)
self.password_entry = tk.Entry(self.password_frame, show='*')
self.password_entry.pack(side=tk.LEFT)
self.login_button = tk.Button(self.master, text='登录', command=self.login)
self.login_button.pack()
def login(self):
username = self.username_entry.get()
password = self.password_entry.get()
if username == 'AAA' and password == '111':
self.master.destroy()
root = tk.Tk()
app = MainFrame(root)
app.mainloop()
else:
tk.messagebox.showerror('错误', '用户名或密码错误!')
```
这里使用了Frame来将Label和Entry放在同一行,将Label和Entry分别放在不同的Frame中,并将两个Frame分别放在不同的行中。你可以将side的值设置为tk.LEFT或tk.RIGHT以调整标签和输入框的位置。
阅读全文