python3.5 tkinter实现页面跳转

迷离的夜空,灰黑色的天际悬着一轮金色的孤舟,载着满仓的梦想,驶向云层深处;凉风微微的吹袭着黑夜的素纱,掸掉岁月的风尘;融融的月光静静的流淌,冲断记忆的决堤;零星点点,柔柔的洒在树叶上,泛出浅浅的绿;如华的银色裹住一丝清凉,铺设一地的奢华。

本文实例为大家分享了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实现页面跳转到此结束。激流勇进者方能领略江河源头的奇观胜景。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python3内置模块之json编码解码方法详解