Tkinter教程之messagebox 弹窗

2018年4月10日00:35:35 发表评论 2,018 views

Tkinter教程之messagebox 弹窗

import tkinter as tk
import tkinter.messagebox
import tkinter.filedialog
from tkinter import colorchooser

window = tk.Tk()
window.title('my window')
window.geometry('200x200')

def hit_me():
    tk.messagebox.showinfo(title='Hi', message='hahahaha')   # return 'ok',提示消息框
    tk.messagebox.showwarning(title='Hi', message='nononono')   # return 'ok',消息警告框
    tk.messagebox.showerror(title='Hi', message='No!! never')   # return 'ok',错误消息框
    print(tk.messagebox.askquestion(title='Hi', message='hahahaha'))   # return 'yes' , 'no'
    print(tk.messagebox.askyesno(title='Hi', message='hahahaha'))   # return True, False
    print(tk.messagebox.askretrycancel(title='Hi', message='hahahaha'))   # return True, False
    print(tk.messagebox.askokcancel(title='Hi', message='hahahaha'))   # return True, False
    print(tk.messagebox.askyesnocancel(title="Hi", message="haha"))     # return, True, False, None

'''filedialog  文件对话框
用来获取文件路径和文件名'''
def readfile():
    tkinter.filedialog.askopenfilename(title='打开CGNS文件',initialdir='C:\',filetypes=[('CGNSdat', '*.dat'), ('All Files', '*')])
''' self.file_opt = options = {}
        options['defaultextension'] = '.txt'
        options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
        options['initialdir'] = 'C:\'
        options['initialfile'] = 'myfile.txt'
        options['parent'] = root
        options['title'] = 'This is a title'
'''
def readcolor():
    print(tkinter.colorchooser.askcolor())
    print(tkinter.colorchooser.askcolor()[1])
    #第一个返回的是浮点数RGU,第二个是16进制
tk.Button(window, text='hit me', command=hit_me).pack()
tk.Button(window, text='readfile', command=readfile).pack()
tk.Button(window, text='readcolor', command=readcolor).pack()


window.mainloop()

运行结果:

Tkinter教程之messagebox 弹窗

51changxue

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: