Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从tkinter中的文件1按下按钮时如何跳转运行/执行文件2_Python_Python 3.x_Tkinter_Gtk_Tk - Fatal编程技术网

Python 从tkinter中的文件1按下按钮时如何跳转运行/执行文件2

Python 从tkinter中的文件1按下按钮时如何跳转运行/执行文件2,python,python-3.x,tkinter,gtk,tk,Python,Python 3.x,Tkinter,Gtk,Tk,我有2python文件file1和file2,它们是用tkinter编码的 文件1是欢迎页面有一个按钮以医生身份登录 文件2是医生将在其中输入其凭证的登录页面。 两个文件都用tkinter编码 运行:- 欢迎文件(文件1)将以GUI形式运行 按下文件1上的按钮时,应将其重定向到 登录页面(文件2) 注:- 两个文件都不同 文件1 Welcome.py 文件2 login.py 按下欢迎页面的按钮登录页面应以gui形式执行 请帮助我有一个大学小项目,但我被困在这里了…… 这绝对不是从主窗口打开第二

我有2python文件file1和file2,它们是用tkinter编码的

文件1
欢迎页面
有一个按钮以医生身份登录

文件2是医生将在其中输入其凭证的
登录页面。
两个文件都用tkinter编码

运行:-

  • 欢迎文件(文件1)将以GUI形式运行
  • 按下文件1上的按钮时,应将其重定向到 登录页面(文件2)
  • 注:-

    两个文件都不同

    文件1 Welcome.py

    文件2 login.py

    按下欢迎页面的按钮登录页面应以gui形式执行

    请帮助我有一个大学小项目,但我被困在这里了……

  • 这绝对不是从主窗口打开第二个窗口的正确方法。在Tkinter中,如果您需要添加窗口,我们将使用
    Toplevel()
    而不是使用另一个
    Tk()
    ,尽管它可以工作,但不是推荐的方法

  • 这里不需要
    importlib
    ,无需使用它即可轻松完成

    就像您可以在file.py中定义函数一样,
    在主文件中导入文件
    ,然后按文件1中的按钮通过
    文件调用它。运行\u登录

    示例:

    def doc():
        A = Application(master = root)
    
    文件2

    def Run_Login():
        # creating the object
        root = Toplevel()
    
        # resolution of the window
        root.geometry("1520x790+0+0")
        root.title ("ABC HOSPITAL")
        # root.iconbitmap('hospital.ico')
    
        # preventing the resize feature
        root.resizable(False, False)
        Application(root)
    
    文件2:

    import file
    
    def doc():
        file.Run_Login(master=root)
    
  • 此外,在应用程序类中继承Toplevel将使您的工作更加轻松,因为这样您就可以从文件导入应用程序中导入文件1
    中的类了。然后在doc中你可以调用它

    示例:

    def doc():
        A = Application(master = root)
    

  • 完整代码 文件1-Window.py

    #import modules
    from tkinter import *
    import random
    from tkinter import messagebox
    
    # Make sure you import name is same as the file name.
    from login import Application
    
    # 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():
        App = Application(root)
    
    #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=======================================================
    login = Button(font=('arial' , 20 , 'bold'),bd=14, text="DOCTOR's LOGIN", 
            fg='white',bg='#04062c',width=27,height=2, command=doc)
    login.place(x=4,y=300)
    
    mainloop() 
    
    #import modules
    from tkinter import *
    import mysql.connector
    from mysql.connector import errorcode
    import random
    from tkinter import messagebox
    
    
    #tkinter window
    class Application(Toplevel):
    
    #funtion for main frames=====================================================================================================================================================================
    
        def __init__(self, master=None):
            Toplevel.__init__(self, master)
            # resolution of the window
            self.geometry("1520x790+0+0")
            self.title ("ABC HOSPITAL")
            self.iconbitmap('hospital.ico')
    
            # preventing the resize feature
            self.resizable(False, False)
    
            # creating the frames in the master
            self.left = Frame(self, width= 1600, height= 900, bg='lightblue',relief=SUNKEN)
            self.left.pack(side=TOP)
    
            #Background Picture
            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)
    
    
    #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
            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)
    
            # 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)
    
    
    if __name__ == "__main__":    
         # Only runs when you run login.py if you import this file whatever after this if statement will not run.
         Application().mainloop()
    

    文件2-login.py

    #import modules
    from tkinter import *
    import random
    from tkinter import messagebox
    
    # Make sure you import name is same as the file name.
    from login import Application
    
    # 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():
        App = Application(root)
    
    #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=======================================================
    login = Button(font=('arial' , 20 , 'bold'),bd=14, text="DOCTOR's LOGIN", 
            fg='white',bg='#04062c',width=27,height=2, command=doc)
    login.place(x=4,y=300)
    
    mainloop() 
    
    #import modules
    from tkinter import *
    import mysql.connector
    from mysql.connector import errorcode
    import random
    from tkinter import messagebox
    
    
    #tkinter window
    class Application(Toplevel):
    
    #funtion for main frames=====================================================================================================================================================================
    
        def __init__(self, master=None):
            Toplevel.__init__(self, master)
            # resolution of the window
            self.geometry("1520x790+0+0")
            self.title ("ABC HOSPITAL")
            self.iconbitmap('hospital.ico')
    
            # preventing the resize feature
            self.resizable(False, False)
    
            # creating the frames in the master
            self.left = Frame(self, width= 1600, height= 900, bg='lightblue',relief=SUNKEN)
            self.left.pack(side=TOP)
    
            #Background Picture
            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)
    
    
    #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
            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)
    
            # 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)
    
    
    if __name__ == "__main__":    
         # Only runs when you run login.py if you import this file whatever after this if statement will not run.
         Application().mainloop()
    


    我希望这能解决你的问题。

    嗨,朴智星!在login.py中检查您的按钮命令,似乎您想要调用按钮本身,这没有任何意义。您可能在文件2中的应用程序类中缺少了
    login()
    函数。实际上,这是一种未命中类型。我尝试了很多方法,因此它错误地保留了下来。我将其删除了。对不起。。。。。。。。。。。。。你知道我如何解决上述问题吗?非常感谢Saad