[英]How to replace specified lines from file1 with lines from file2 in python
[英]How jump run/execute file2 when a button is pressed from file1 in tkinter
我有2個在tkinter中編碼的python文件File 1和File 2 。
文件1是Welcome page
具有1個按鈕,以醫生身份登錄。
文件2是醫生將在其中輸入其憑據的login page
。 這兩個文件都在tkinter中編碼
正在運行:-
注意:-
這兩個文件是不同的 。
文件1 Welcome.py
#import modules
from tkinter import *
import random
from tkinter import messagebox
import importlib
# 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():
importlib.import_module('login')
#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)
login.place(x=4,y=300)
root.mainloop()
文件2 login.py
#import modules
from tkinter import *
import mysql.connector
from mysql.connector import errorcode
import random
from tkinter import messagebox
# creating the object
root = Tk()
# 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)
#tkinter window
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
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)
b = Application(root)
root.mainloop()
點擊歡迎頁面上的按鈕登錄頁面應以gui形式執行
請幫助我有一個大學迷你項目,但是我被困在這里。
絕對不是從主窗口打開第二個窗口的正確方法。 在Tkinter中,如果您需要其他窗口,我們可以使用Toplevel()
而不是另一個Tk()
,盡管它可以工作,但不建議使用。
您無需在這里使用importlib
,無需使用它即可非常輕松地完成。
就像你可以在你的file.py定義一個函數, import file
在你的主文件,然后一個按鈕,在文件1新聞界稱之為file.Run_login
例:
文件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)
同樣,在Application類中繼承Toplevel將使您的工作更加輕松,因為您可以from file import Application
中將類導入File 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()
文件2-login.py
#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()
我希望這能解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.