热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python3图形界面简单案例(tkinter)

1.对于python3中对话框的使用:注意python3中相对于python2有很多的变化,其中一些包的名字是改变了,比图Tkinter变为tkinter,而对于对话框在python2中可

1.对于python3中对话框的使用:

注意python3中相对于python2有很多的变化,其中一些包的名字是改变了,比图Tkinter变为 tkinter,而对于对话框在python2中可以通过导入tkMessageBox来使用,比如:
tkMessageBox.showifo("messagebox","this is a messagebox")

在python3中此模块变为了messagebox,我们只需要导入tkinter.messagebox就可以使用(tk = Tk()):

tk.messagebox.showinfo("messagebox","this is a messagebox")

参考一下案例:

from tkinter import *
import tkinter.messagebox

class MainWindow:

def buttonListener1(self,event):
tkinter.messagebox.showinfo("messagebox","this is button 1 dialog")
def buttonListener2(self,event):
tkinter.messagebox.showinfo("messagebox","this is button 2 dialog")
def buttonListener3(self,event):
tkinter.messagebox.showinfo("messagebox","this is button 3 dialog")
def buttonListener4(self,event):
tkinter.messagebox.showinfo("messagebox","this is button 4 dialog")

def __init__(self):
self.frame = Tk()

self.button1 = Button(self.frame,text = "button1",width = 10,height = 5)
self.button2 = Button(self.frame,text = "button2",width = 10,height = 5)
self.button3 = Button(self.frame,text = "button3",width = 10,height = 5)
self.button4 = Button(self.frame,text = "button4",width = 10,height = 5)

self.button1.grid(row = 0,column = 0,padx = 5,pady = 5)
self.button2.grid(row = 0,column = 1,padx = 5,pady = 5)
self.button3.grid(row = 1,column = 0,padx = 5,pady = 5)
self.button4.grid(row = 1,column = 1,padx = 5,pady = 5)

self.button1.bind("",self.buttonListener1)
self.button2.bind("",self.buttonListener2)
self.button3.bind("",self.buttonListener3)
self.button4.bind("",self.buttonListener4)

self.frame.mainloop()

window = MainWindow()

以上通过定义类的方式来显示了一个图形界面(其中值得注意的是self这个参数)

运行结果如下:

python3模块变化地址参考:

http://docs.pythonsprints.com/python3_porting/py-porting.html

python控件与布局参考地址:

http://effbot.org/tkinterbook/tkinter-index.htm#class-reference

二用python写的图形界面例子:

from tkinter import *

class MainWindow:
def __init__(self):
self.frame = Tk()

self.label_name = Label(self.frame,text = "name:")
self.label_age = Label(self.frame,text = "age:")
self.label_sex = Label(self.frame,text = "sex:")

self.text_name = Text(self.frame,height = "1",width = 30)
self.text_age = Text(self.frame,height = "1",width = 30)
self.text_sex = Text(self.frame,height = "1",width = 30)

self.label_name.grid(row = 0,column = 0)
self.label_age.grid(row = 1,column = 0)
self.label_sex.grid(row = 2,column = 0)

self.button_ok = Button(self.frame,text = "ok",width = 10)
self.button_cancel = Button(self.frame,text = "cancel",width = 10)

self.text_name.grid(row = 0,column = 1)
self.text_age.grid(row = 1,column = 1)
self.text_sex.grid(row = 2,column = 1)

self.button_ok.grid(row = 3,column = 0)
self.button_cancel.grid(row = 3,column = 1)

self.frame.mainloop()

frame = MainWindow()
运行结果:




推荐阅读
author-avatar
从1前2有3个4人_706
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有