您的位置:首页 > 编程语言 > Python开发

Python -- Gui编程 -- Tkinter的使用 -- 对话框消息框

2014-05-04 16:43 816 查看
1.消息框

tkMessageBox.py

import tkinter
from tkinter import messagebox

def cmd():
global n
global buttontext
n += 1
if n==1:
messagebox.askokcancel('Python Tkinter', 'askokcancel')
buttontext.set('askquestion')
elif n==2:
messagebox.askquestion('Python Tkinter', 'askquestion')
buttontext.set('askyesno')
elif n==3:
messagebox.askyesno('Python Tkinter', 'askyesno')
buttontext.set('showerror')
elif n==4:
messagebox.showerror('Python Tkinter', 'showerror')
buttontext.set('showinfo')
elif n==5:
messagebox.showinfo('Python Tkinter', 'showinfo')
buttontext.set('showwarning')
else:
n = 0
messagebox.showwarning('Python Tkinter', 'showwarning')
buttontext.set('askokcancel')

n = 0
root = tkinter.Tk()
buttontext = tkinter.StringVar()
buttontext.set('askokcancel')
button = tkinter.Button(root, textvariable=buttontext, command=cmd)
button.pack()
root.mainloop()




2.简单对话框

tkSimpleDialog.py

import tkinter
from tkinter import simpledialog

def inputStr():
r = simpledialog.askstring('Python Tkinter', 'Input String', initialvalue = 'Python Tkinter')
print(r)
def inputInt():
r = simpledialog.askinteger('Python Tkinter', 'Input Integer')
print(r)
def inputFloat():
r = simpledialog.askfloat('Python Tkinter', 'Input Float')
print(r)

root = tkinter.Tk()
btn1 = tkinter.Button(root, text='Input String', command=inputStr)
btn2 = tkinter.Button(root, text='Input Integer', command=inputInt)
btn3 = tkinter.Button(root, text='Input Float', command=inputFloat)

btn1.pack(side='left')
btn2.pack(side='left')
btn3.pack(side='left')

root.mainloop()




3.文件对话框

tkFileDialog.py

import tkinter
from tkinter import filedialog

def openfile():
r = filedialog.askopenfilename(title='打开文件', filetypes=[('Python', '*.py *.pyw'), ('All Files', '*')])
print(r)
def savefile():
r = filedialog.asksaveasfilename(title='保存文件', initialdir='d:\mywork', initialfile='hello.py')
print(r)

root = tkinter.Tk()
btn1 = tkinter.Button(root, text='File Open', command=openfile)
btn2 = tkinter.Button(root, text='File Save', command=savefile)

btn1.pack(side='left')
btn2.pack(side='left')
root.mainloop()


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: