Tkinter教程之messagebox 弹窗

畅学网 Python评论2,555阅读模式

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 弹窗

 
畅学网
  • 本文由 畅学网 发表于 2018年4月10日00:35:35
  • 转载请务必保留本文链接:https://51changxue.com/455.html
Python

pycharm永久破解激活教程

PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试、语法高亮、Project管理、代码跳转、智能提示、自动完成、...

发表评论