您好,登錄后才能下訂單哦!
本文實例為大家分享了tkinter實現頁面跳轉的具體代碼,供大家參考,具體內容如下
主函數main.py
from tkinter import * from LoginPage import * root = Tk() root.title('小程序') LoginPage(root) root.mainloop()
登陸界面函數LoginPage.py
from tkinter import * from tkinter.messagebox import * from MainPage import * class LoginPage(object): def __init__(self, master=None): self.root = master #定義內部變量root self.root.geometry('%dx%d' % (300, 180)) #設置窗口大小 self.username = StringVar() self.password = StringVar() self.createPage() def createPage(self): self.page = Frame(self.root) #創建Frame self.page.pack() Label(self.page).grid(row=0, stick=W) Label(self.page, text = '賬戶: ').grid(row=1, stick=W, pady=10) Entry(self.page, textvariable=self.username).grid(row=1, column=1, stick=E) Label(self.page, text = '密碼: ').grid(row=2, stick=W, pady=10) Entry(self.page, textvariable=self.password, show='*').grid(row=2, column=1, stick=E) Button(self.page, text='登陸', command=self.loginCheck).grid(row=3, stick=W, pady=10) Button(self.page, text='退出', command=self.page.quit).grid(row=3, column=1, stick=E) def loginCheck(self): name = self.username.get() secret = self.password.get() if name=='wangliang' and secret=='123456': self.page.destroy() MainPage(self.root) else: showinfo(title='錯誤', message='賬號或密碼錯誤!')
主界面函數MainPage.py
from tkinter import * from view import * #菜單欄對應的各個子頁面 class MainPage(object): def __init__(self, master=None): self.root = master #定義內部變量root self.root.geometry('%dx%d' % (600, 400)) #設置窗口大小 self.createPage() def createPage(self): self.inputPage = InputFrame(self.root) # 創建不同Frame self.queryPage = QueryFrame(self.root) self.countPage = CountFrame(self.root) self.aboutPage = AboutFrame(self.root) self.inputPage.pack() #默認顯示數據錄入界面 menubar = Menu(self.root) menubar.add_command(label='數據錄入', command = self.inputData) menubar.add_command(label='查詢', command = self.queryData) menubar.add_command(label='統計', command = self.countData) menubar.add_command(label='關于', command = self.aboutDisp) self.root['menu'] = menubar # 設置菜單欄 def inputData(self): self.inputPage.pack() self.queryPage.pack_forget() self.countPage.pack_forget() self.aboutPage.pack_forget() def queryData(self): self.inputPage.pack_forget() self.queryPage.pack() self.countPage.pack_forget() self.aboutPage.pack_forget() def countData(self): self.inputPage.pack_forget() self.queryPage.pack_forget() self.countPage.pack() self.aboutPage.pack_forget() def aboutDisp(self): self.inputPage.pack_forget() self.queryPage.pack_forget() self.countPage.pack_forget() self.aboutPage.pack()
子界面函數view,py
from tkinter import * from tkinter.messagebox import * class InputFrame(Frame): # 繼承Frame類 def __init__(self, master=None): Frame.__init__(self, master) self.root = master #定義內部變量root self.itemName = StringVar() self.importPrice = StringVar() self.sellPrice = StringVar() self.deductPrice = StringVar() self.createPage() def createPage(self): Label(self).grid(row=0, stick=W, pady=10) Label(self, text = '藥品名稱: ').grid(row=1, stick=W, pady=10) Entry(self, textvariable=self.itemName).grid(row=1, column=1, stick=E) Label(self, text = '進價 /元: ').grid(row=2, stick=W, pady=10) Entry(self, textvariable=self.importPrice).grid(row=2, column=1, stick=E) Label(self, text = '售價 /元: ').grid(row=3, stick=W, pady=10) Entry(self, textvariable=self.sellPrice).grid(row=3, column=1, stick=E) Label(self, text = '優惠 /元: ').grid(row=4, stick=W, pady=10) Entry(self, textvariable=self.deductPrice).grid(row=4, column=1, stick=E) Button(self, text='錄入').grid(row=6, column=1, stick=E, pady=10) class QueryFrame(Frame): # 繼承Frame類 def __init__(self, master=None): Frame.__init__(self, master) self.root = master #定義內部變量root self.itemName = StringVar() self.createPage() def createPage(self): Label(self, text='查詢界面').pack() class CountFrame(Frame): # 繼承Frame類 def __init__(self, master=None): Frame.__init__(self, master) self.root = master #定義內部變量root self.createPage() def createPage(self): Label(self, text='統計界面').pack() class AboutFrame(Frame): # 繼承Frame類 def __init__(self, master=None): Frame.__init__(self, master) self.root = master #定義內部變量root self.createPage() def createPage(self): Label(self, text='關于界面').pack()
效果圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。