Python GUI tkinter窗口视窗学习2
test4
import tkinter as tk
# 实例化object,建立窗口
window = tk.Tk()
window.title('my test')
# 设置窗口的大小
window.geometry('500x300') # 这里的乘号是小写的x
# 在界面上设置标签
var = tk.StringVar() # 将label标签的内容设置为字符类型,用var来接收hit_me函数的传出内容用以显示在标签上
l = tk.Label(window,textvariable = var,bg = 'olive',width = 30,height = 2)
# 放置标签 两种方法: l.pack() 2. l.place()
l.pack()
# button相关内容
on_hit = False
def click():
global on_hit
if on_hit == False:
on_hit = True
var.set("clicked hhhh")
else:
on_hit = False
var.set('')
# 放置button按键
b = tk.Button(window,text = 'hit',width = 10,height = 1,command = click)
b.pack()
# 主窗口循环显示
window.mainloop()
test5
Entry 输入
# test5
# Entry:输入
import tkinter as tk
window = tk.Tk()
window.title('呵呵')
window.geometry('700x400')
e1 = tk.Entry(window,show = '*') # 显示密文格式
e2 = tk.Entry(window,show = None) # 显示明文格式
e1.pack()
e2.pack()
window.mainloop()
test6
import tkinter as tk
window = tk.Tk()
window.title('hhhh')
window.geometry('700x400')
var1 = tk.StringVar() # 创建变量,接收点击的具体选项的内容
l = tk.Label(window,bg = 'green',fg = 'yellow',width = 10,textvariable = var1)
l.pack()
def print_selection():
value = lb.get(lb.curselection())
var1.set(value)
b1 = tk.Button(window,text = 'print_selection',width = 15,height = 2,command = print_selection)
b1.pack()
var2 = tk.StringVar()
var2.set((1,2,3,4))
# 创建Listbox 把var2的值赋给listbox
lb = tk.Listbox(window,listvariable = var2)
list_items = [11,22,33,44]
for item in list_items:
lb.insert('end',item)
lb.pack()
window.mainloop()
# test8
import tkinter as tk
window = tk.Tk()
window.title('my test8')
window.geometry('700x400')
var = tk.StringVar()
l = tk.Label(window,bg = 'green',width = 20,text = 'empty')
l.pack()
def print_selection():
l.config(text = 'you have selected'+var.get())
r1 = tk.Radiobutton(window,text = 'option1',variable = var,value = '1',command = print_selection)
r1.pack()
r2 = tk.Radiobutton(window,text = 'option2',variable = var,value = '2',command = print_selection)
r2.pack()
r3 = tk.Radiobutton(window,text = 'option3',variable = var,value = '3',command = print_selection)
r3.pack()
window.mainloop()
最后赋的是value的值