本文实例为大家分享了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() 

效果图:

python3.5 tkinter实现页面跳转

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com