message box
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()#Create main window
root.title("Messagebox")
root.geometry("400x300+200+100")
def hello():
tk.messagebox.showinfo("showinfo","Hello World!")
tk.messagebox.askokcancel("askokcancel", "OK or Cancel?")
tk.messagebox.askquestion("askquestion", "Ask Question?")
tk.messagebox.askretrycancel("askretrycancel", "Ask Retry or Cancel?")
tk.messagebox.showerror("showerror", "Show Error")
tk.messagebox.showwarning("showwarning", "Show Warning")
#add button
btn_01 = tk.Button(root,text = "Button",command = hello)
btn_01.pack()
root.mainloop()
Guess Number Game
import tkinter as tk
from tkinter import simpledialog
import random
class Application:
def __init__(self):
self.root = tk.Tk()
self.root.title("Guessing Number Game")
self.root.geometry("260x80+400+300")
self.comp_rand = 0
self.times = 0
self.createWidget()
def createWidget(self):
self.frame = tk.Frame(self.root)
self.frame.pack()
self.label = tk.Label(self.frame,text="Please input a number")
self.label.grid(row=0,column = 0,pady=10)
self.v = tk.StringVar()
self.entry = tk.Entry(self.frame,state="disable",textvariable=self.v)
self.entry.grid(row=0,column=1,pady=10)
self.btn = tk.Button(self.frame,text="start game",command=self.clickbtn)
self.btn.grid(row=1,column=0,columnspan=2,stick="ew")
def clickbtn(self):
if self.btn["text"] == "start game":
self.entry["state"] = "normal"
self.btn["text"] = "Confirm"
while True:
value_min = tk.simpledialog.askinteger("Set Minimum Number","Minimum Number")
if value_min != None:
break
while True:
value_max = tk.simpledialog.askinteger("Set Maximum Number","Maximum Number")
if value_max != None:
break
while True:
self.times = tk.simpledialog.askinteger("Set Guess Times","Times to Guess Number")
if self.times != None:
break
self.comp_rand = random.randint(value_min,value_max)
elif self.btn["text"] == "Confirm":
if self.times > 0:
try:
input_value = int(self.entry.get())
except ValueError:
tk.messagebox.showerror("System Information","You input non-digit number")
else:
self.times -= 1
if input_value == self.comp_rand:
tk.messagebox.showinfo("System Information","Congratulations,Bingo, You win.")
self.entry["state"] = "disable"
self.btn["text"] = "start game"
else:
if input_value > self.comp_rand:
tk.messagebox.showerror("System Information","You guessed number is greater")
else:
tk.messagebox.showerror("System Information","You guessed number is less")
if self.times == 0:
tk.messagebox.showinfo("System Information",\
"Sorry,you guessed too much times. You lose.")
self.entry["state"] = "disable"
self.btn["text"] = "start game"
else:
tk.messagebox.showinfo("System Information",\
"You have "+str(self.times)+" chances.")
def run(self):
self.root.mainloop()
app = Application()
app.run()
[…] GUI interface example exercises| |____Unit 77 event handling| |____Unit 78 Remainder Gadget| |____Unit 79 tkinter version guessing number game| |____Unit 80 tkinter more components| |____Unit 81 Simple Notepad (Part 1)| |____Unit 82 Simple […]