import tkinter as tk
from tkinter import Label, font
import tkinter.messagebox as messagebox
font_str = ('Arial', 20)
def InitWindow(_window):
global window
window = _window
def DrawTest():
DrawLabel()
DrawButton()
DrawEntry()
DrawText()
DrawListbox()
DrawRadioButton()
DrawCheckButton()
DrawScale()
DrawCanvas()
DrawMenu()
DrawFrame()
DrawMessageBox()
def DrawLabel():
print("draw lable")
global var
var = tk.StringVar()
l = tk.Label(window, textvariable=var, bg="green", font=font_str, width=30, height=2)
l.pack()
def DrawButton():
print("draw button")
b = tk.Button(window, text="hit me", width=10, height=1, command=hit_me)
b.pack()
on_hit = False
def hit_me():
global on_hit
if on_hit == False:
var.set("hit me")
else:
var.set("")
on_hit = not on_hit
def DrawEntry():
print("draw entry")
e1 = tk.Entry(window, show="*", font=font_str)
e1.pack()
e2 = tk.Entry(window, show=None, font=font_str)
e2.pack()
def DrawText():
print("draw text")
b1 = tk.Button(window, text="insert", width=10, height=1, command=on_inset)
b1.pack()
b2 = tk.Button(window, text="end", width=10, height=1, command=on_end)
b2.pack()
global e
e = tk.Entry(window, show=None, font=font_str)
e.pack()
global t
t = tk.Text(window, font=font_str, height=3)
t.pack()
def on_inset():
var_text = e.get()
t.insert("insert", var_text)
def on_end():
var_text = e.get()
t.insert("end", var_text)
def DrawListbox():
print("draw list box")
global var_listbox
var_listbox = tk.StringVar()
l_listbox = tk.Label(window, bg="green", fg="yellow", font=font_str, width=10, textvariable=var_listbox)
l_listbox.pack()
b_listbox = tk.Button(window, text="print selection", width=15, height=2, command=print_selection_lb)
b_listbox.pack()
list_items = ["aa", "bb", "cc", "dd"]
global lb
var_listbox2 = tk.StringVar()
var_listbox2.set((1, 2, 3, 4))
lb = tk.Listbox(window, listvariable=var_listbox2)
for item in list_items:
lb.insert("end", item)
lb.insert(1, "first")
lb.insert(2, "second")
lb.delete(2)
lb.pack()
def print_selection_lb():
value = lb.get(lb.curselection())
var_listbox.set(value)
def DrawRadioButton():
print("draw radio button")
global var_rb
var_rb = tk.StringVar()
global l_rb
l_rb = tk.Label(window, bg="yellow", width=20, text="empty")
l_rb.pack()
rb1 = tk.Radiobutton(window, text="选项1", variable=var_rb, value="A", command=print_selection_rb)
rb1.pack()
rb2 = tk.Radiobutton(window, text="选项2", variable=var_rb, value="B", command=print_selection_rb)
rb2.pack()
rb3 = tk.Radiobutton(window, text="选项3", variable=var_rb, value="C", command=print_selection_rb)
rb3.pack()
def print_selection_rb():
l_rb.config(text="You have selected " + var_rb.get())
def DrawCheckButton():
print("draw check button")
global l_cb
l_cb = tk.Label(window, bg="yellow", width=20, text="empty")
l_cb.pack()
global var_cb1, var_cb2
var_cb1 = tk.IntVar()
var_cb2 = tk.IntVar()
cb1 = tk.Checkbutton(window, text="Python", variable=var_cb1, onvalue=1, offvalue=0, command=print_selection_cb)
cb1.pack()
cb2 = tk.Checkbutton(window, text="C++", variable=var_cb2, onvalue=1, offvalue=0, command=print_selection_cb)
cb2.pack()
def print_selection_cb():
if (var_cb1.get() == 1) & (var_cb2.get() == 0):
l_cb.config(text="I love only python")
elif (var_cb1.get() == 0) & (var_cb2.get() == 1):
l_cb.config(text="I love only c++")
elif (var_cb1.get() == 0) & (var_cb2.get() == 0):
l_cb.config(text="I do not love either")
else:
l_cb.config(text="I love both")
def DrawScale():
print("draw scale")
def DrawCanvas():
print("draw canvas")
def DrawMenu():
print("draw menu")
menubar = tk.Menu(window)
fileMenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="Open", command=do_job)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", command=window.quit)
editMenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Cut", command=do_job)
editMenu.add_command(label="Copy", command=do_job)
editMenu.add_command(label="Paste", command=do_job)
window.config(menu=menubar)
def do_job():
print("do_job")
def DrawFrame():
print("draw frame")
def DrawMessageBox():
print("draw message box")
b_mb = tk.Button(window, text="Message Box", font=font_str, command=hit_me_mb)
b_mb.pack()
def hit_me_mb():
messagebox.showerror(title="Error!!", message="出错了!!")