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()
运行结果: