代码语言:javascript复制
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('200x200')
def hit_me():
#tk.messagebox.showinfo(title='Hi', message='hahahaha')
#tk.messagebox.showwarning(title='Hi', message='nononono')
#tk.messagebox.showerror(title='Hi', message='No!! never')
#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.asktrycancel(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
tk.Button(window, text='hit me', command=hit_me).pack()
window.mainloop()
module 'tkinter' has no attribute 'messagebox'
- 注意,这个问题是在windows系统上
python3.5.0
出现的错误。 - 解决方案是
import tkinter.messagebox
module 'tkinter.messagebox' has no attribute 'asktrycancel'
- 注意:在新版本的Tkinter中,
已经将方法asktrycancel改为askretrycancel
- 提示信息框
tk.messagebox.showinfo(title='Hi', message='hahahaha')
- 警告框
tk.messagebox.showwarning(title='Hi', message='nononono')
- 错误框
tk.messagebox.showerror(title='Hi', message='No!! never')
- 询问框
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.askyesnocancel(title="Hi", message="haha")) # return, True, False, None