如何将小部件放在不同的框架中?

时间:2019-01-24 16:12:15

标签: python user-interface tkinter

我在这里使用示例代码,以便为我的应用程序(一个没有类的应用程序)创建一个登录框架。

Switch between two frames in tkinter

在此代码中,表明应该将框架作为参数传递给小部件,以便指定其框架。但是,当我在下面将f1作为参数传递时,“昵称”标签消失了。登录屏幕中的其他元素/小部件也是如此。我该如何纠正?

import tkinter
from tkinter import messagebox

def gantihal(frame):
    frame.tkraise()

root = tkinter.Tk()
root.geometry("500x500")


f1 = tkinter.Frame(root)
f2 = tkinter.Frame(root)


for frame in (f1, f2):
    frame.grid(row=0, column=0, sticky='news')



nickname_label= tkinter.Label(f1, text="Nickname:").place(x=140, y=150)
password_label = tkinter.Label(text="Password:").place(x=140, y=200)

nickname_area = tkinter.Entry()
nickname_area.place(x=210, y=150)
password_area = tkinter.Entry(show="*")
password_area. place(x=210, y=200)


def auth_login(event=None):
    global nickname_area
    global password_area
    username = nickname_area.get()
    pswd = password_area.get()
    if username == "admin" and pswd == "root":
        try:
            gantihal(f2)
            messagebox._show("Rchat v1.0.", "Welcome to RChat !")
        except ConnectionRefusedError:
            messagebox.showerror("Server Error", "The server is not connected.")

    else:
        messagebox.showerror("Wrong credentials", "Either nickname or password is wrong. Please try again.")


login_button = tkinter. Button(text="Log In" , command= auth_login, height=2, width=20)
login_button.place(x=195, y=250)


gantihal(f1)
root.mainloop()

0 个答案:

没有答案