You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter中点击Welcome.py按钮如何跳转执行login.py页面?

Fixing the Button Navigation Between Welcome.py and login.py

Hey there! Let's get that doctor login button working properly. The issue comes from three small oversights in your code—let's break down the problems and fix them step by step.

What's Going Wrong?

  • Your button isn't connected to the doc() function: Right now, the "DOCTOR's LOGIN" button doesn't have any command attached, so clicking it does nothing.
  • Duplicate Tkinter root windows: Both files create their own root = Tk() instance, which causes conflicts when you import login.py directly.
  • Login code runs immediately on import: Your login.py code executes as soon as you import it, which isn't what you want—you want to trigger it only when the button is clicked.

Step-by-Step Fixes

1. Update Welcome.py to Bind the Button and Manage Windows

Modify the button to call your doc() function, and update the doc() function to close the welcome window before launching the login page:

#import modules
from tkinter import *
import random
from tkinter import messagebox
import login  # Import directly instead of importlib for simplicity

# creating the object
root = Tk()
# resolution of the window
root.geometry("500x540+500+100")
root.title ("ABC HOSPITAL")
# preventing the resize feature
root.resizable(False, False)

def doc():
    root.destroy()  # Close the welcome window
    login.run_login()  # Launch the login window

#LABELS=====================================================
heading = Label(font=('times new roman' , 25 , 'bold'), text="WELCOME TO ABC HOSPITAL", fg='black', bg='#fbf9d3')
heading.place(x=3, y=10)
heading = Label(font=('times new roman' , 22 , 'bold'), text="Choose Login", fg='black', bg='#fbf9d3')
heading.place(x=150, y=250)

#button to perform a command=======================================================
# Add command=doc to bind the button to your function
login_btn = Button(font=('arial' , 20 , 'bold'),bd=14, text="DOCTOR's LOGIN", fg='white',bg='#04062c',width=27,height=2, command=doc)
login_btn.place(x=4,y=300)

root.mainloop()

2. Refactor login.py to Encapsulate the Login GUI

Wrap your login window code in a function so you can call it from Welcome.py, and add a guard clause to prevent it from running automatically on import:

#import modules
from tkinter import *
import mysql.connector
from mysql.connector import errorcode
import random
from tkinter import messagebox

#tkinter window class
class Application:
    #funtion for main frames=====================================================================================================================================================================
    def __init__(self, master):
        self.master = master
        # creating the frames in the master
        self.left = Frame(master, width= 1600, height= 900, bg='lightblue',relief=SUNKEN)
        self.left.pack(side=TOP)
        #Background Picture
        try:
            self.photo1 = PhotoImage(file='background.png')
            self.pic = Label(self.left, font=('arial' , 1 , 'bold'), image= self.photo1)
            self.pic.place(x=0, y=0)
        except:
            # Add a fallback if the image file is missing
            Label(self.left, text="Background image missing", bg='lightblue').place(x=0,y=0)
        #LABELS=====================================================
        self.heading = Label(self.left,font=('arial' , 50 , 'bold'), text="ABC Hospital", fg='black', bg='#06378b' ,anchor='w')
        self.heading.place(x=550, y=0)
        #Login Picture
        try:
            self.photo = PhotoImage(file= 'login.png')
            self.pic = Label(self.left, font=('arial' , 40 , 'bold'), image= self.photo ,fg = "lightblue", bg='#06378b')
            self.pic.place(x=640, y=100)
        except:
            Label(self.left, text="Login image missing", bg='#06378b', fg='black').place(x=640,y=100)
        # user name
        self.username = Label(self.left, text="Username", font=('arial 30 bold'), fg='black', bg='#063998')
        self.username.place(x=550, y=350)
        # password
        self.password = Label(self.left, text="Password", font=('arial 30 bold'), fg='black', bg='#063998')
        self.password.place(x=550, y=410)
        #TEXTBOX=====================================================
        #username
        self.username_ent = Entry(self.left,font=('arial' , 20 , 'bold'))
        self.username_ent.place(x=750, y=360)
        #password
        self.password_ent = Entry(self.left, font=('arial' , 20 , 'bold'),show='*')
        self.password_ent.place(x=750, y=420)
        # button to perform a command================================
        #button1
        self.login = Button(self.left,font=('arial' , 20 , 'bold'), text="LOGIN", bg='steelblue')
        self.login.place(x=700, y=480)

def run_login():
    # Create the login window only when this function is called
    root = Tk()
    root.geometry("1520x790+0+0")
    root.title ("ABC HOSPITAL")
    try:
        root.iconbitmap('hospital.ico')
    except:
        pass  # Ignore if icon file is missing
    root.resizable(False, False)
    b = Application(root)
    root.mainloop()

# Only run the login window if this file is executed directly (not imported)
if __name__ == "__main__":
    run_login()

Why This Works

  • Button binding: The command=doc parameter makes the button trigger your function when clicked.
  • Single root window: We close the welcome window with root.destroy() before launching the login window, avoiding conflicting Tk instances.
  • Encapsulated login code: By wrapping the login GUI in run_login(), we control exactly when it runs—only when the button is clicked.

This should give you the smooth page transition you need for your hospital project!

内容的提问来源于stack exchange,提问作者H.Park

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:24:10