Tkinter:父窗口屏幕变化

分享于2022年07月17日 python tkinter 问答
【问题标题】:Tkinter:父窗口屏幕变化(Tkinter : parent windows screen change)
【发布时间】:2022-01-27 05:27:26
【问题描述】:

我对 Tkinter 比较陌生,需要帮助。

当从父窗口单击按钮(登录)时,我创建了一个连接数据库窗口。新窗口是 login.py(file)。但是打不开

连接下面的database.py;

from tkinter import *
from tkinter import ttk
from PIL import Image,ImageTk 
import os 
import pickle 
import mysql.connector  as sql
from tkinter import messagebox
import login



def login1():
    host = host_entry.get()
    port = port_entry.get()
    username = username_entry.get()
    password = password_entry.get()
    database="cars"

    spec=sql.connect(host=host,user=username,password=password,port=port)
    if spec.is_connected():
        messagebox.showinfo("Connected","Database connected Sucessfully")
    else:
        messagebox.showerror("Exists", "Database is already connected")
    spec.close()




root = Tk()
root.geometry("1067x600")
root.configure(background="black")
root.resizable(False, False) 
root.title("School Diaries")


#background image 
bg = ImageTk.PhotoImage(file="files\Sublime Light1.jpg")
lbl_bg = Label(root,image=bg)
lbl_bg.place(x=0,y=0,relwidth=1,relheight=1)

#Labels 
host_label = Label(root, text="Host Name ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 12, "bold"))
host_label.place(x=675, y=115)
host_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#host_entry.insert(0, "localhost")
host_entry.place(x=687, y=139, width=145)

port_label = Label(root, text="Port ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
port_label.place(x=675, y=190)
port_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#port_entry.insert(0, "3307")
port_entry.place(x=690, y=213, width=145)

username_label = Label(root, text="Username ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
username_label.place(x=675, y=265)
username_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#username_entry.insert(0, "root")
username_entry.place(x=687, y=287, width=145)

password_label = Label(root, text="Password ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
password_label.place(x=675, y=338)
password_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#password_entry.insert(0, "root")
password_entry.place(x=687, y=361, width=145)

#buttons
submit = ImageTk.PhotoImage(file='Pics\submit.png')
submit_button = Button(root, image=submit,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white",borderwidth=0, background="white", cursor="hand2",command=login1)
submit_button.place(x=655, y=440)

login = ImageTk.PhotoImage(file='Pics\login.png')
login_button = Button(root, image=login,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white",borderwidth=0, background="white", cursor="hand2",commmand=login)
login_button.place(x=785, y=442)


root.mainloop()

image of connect database login.py 文件如下;

from tkinter import *
from tkinter import ttk
from PIL import Image,ImageTk 
import os 
import pickle 
import mysql.connector  as sql
from tkinter import messagebox
import user_inter 

def login():
    host = user_inter.host_entry.get()
    port = user_inter.port_entry.get()
    username = username_entry.get()
    password = password_entry.get()


    spec=sql.connect(host=host,user=username,password=password,port=port)
    if spec.is_connected():
        messagebox.showinfo("Connected","Database connected Sucessfully")
    else:
        messagebox.showerror("Exists", "Failed")
    

    mycur=spec.cursor()
    mycur.execute()

    

root = Tk()
root.geometry("1067x600")
root.configure(background="black")
root.resizable(False, False) 
root.title("School Diaries")

#background
bg = ImageTk.PhotoImage(file="files\login.jpg")
lbl_bg = Label(root,image=bg)
lbl_bg.place(x=0,y=0,relwidth=1,relheight=1)


#Labels
login_lbl = Label(root, text="Login", bg="white", fg="#4f4e4d",font=("San Francisco", 45))
login_lbl.place(x=757,y=120)

username_label = Label(root, text="Username ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
username_label.place(x=675, y=190)
username_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#username_entry.insert(0, "root")
username_entry.place(x=695, y=215, width=190)

password_label = Label(root, text="Password ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
password_label.place(x=675, y=280)
password_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#password_entry.insert(0, "root")
password_entry.place(x=692, y=305, width=190)

login = ImageTk.PhotoImage(file='Pics\login.png')
login_button = Button(root, image=login,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white",borderwidth=0, background="white", cursor="hand2",command=login)
login_button.place(x=770, y=390)

root.mainloop()

image of login page

PS ; connect database.py 在代码中被称为 user_intern.py

0

现在我的疑问是; 我已经使用 tkinter 创建了一个 login.py 页面,并且我还创建了一个连接 database.py 页面,所以我怀疑我在连接数据库窗口中有一个名为 login 的按钮,现在如何给出参数,例如如果我单击登录按钮屏幕shd更改为我的login.py屏幕.....希望你理解我的疑问

当我点击登录时,login.py shd 在父窗口本身中执行

谢谢,

欢迎对代码进行任何编辑以获取我的输出 当我在连接数据库中单击登录时,登录窗口会在父窗口本身中打开

  • 你的第一个错误,你使用了两个根。主循环需要只有一个,它应该在主 py 文件中。您还相互导入了两者。这是另一个错误。您应该将调用文件导入主文件。
  • 你已经用 ImageTk.PhotoImage() 的实例覆盖了 login

【解决方案1】:

@ablmmcu 在评论中提到,tkinter 只有 1 个根,它应该在主程序中 另一点是,我建议使用 tkinter.TopLevel() 并在您按下 login 按钮并成功登录时将其用作数据库的窗口。 更多关于 TopLevel 的信息:

Toplevel widgets work as windows that are directly managed by the window manager. They do not necessarily have a parent widget on top of them.

Your application can use any number of top-level windows.

来源: https://www.tutorialspoint.com/python/tk_toplevel.htm

附带说明:您将 TopLevel 窗口放在根目录中。