tkinter实例讲解,让学习便的更轻松

#记录学习历程,thinker,作者:author: Burkhard A. Meier
#版权归原作者所有

imports

#======================
import tkinter as tk #导入tkinter,并命名为tk,后续好书写
from tkinter import ttk #导入tkinter中的ttk模块
from tkinter import scrolledtext#导入tkinter中的scrolledtext模块,多行文本

Create instance

win = tk.Tk() #创建实例

Add a title

win.title(“Python GUI”) #增加标题

Modify adding a Label

a_label = ttk.Label(win, text=“A Label”) #增加一个标题
a_label.grid(column=0, row=0) 网络管理器显示在第一列第一行;

Modified Button Click Function

def click_me():#创建点击事件调用程序
action.configure(text='Hello ’ + name.get() + ’ ’ +
number_chosen.get())

Changing our Label

ttk.Label(win, text=“Enter a name:”).grid(column=0, row=0)#创建个提示label

Adding a Textbox Entry widget

name = tk.StringVar() #创建个字符串变量,可保证变量的值及时显示到屏幕上
name_entered = ttk.Entry(win, width=12, textvariable=name)
name_entered.grid(column=0, row=1)

Adding a Button

action = ttk.Button(win, text=“Click Me!”, command=click_me)
action.grid(column=2, row=1) # <= change column to 2

Creating three checkbuttons

ttk.Label(win, text=“Choose a number:”).grid(column=1, row=0)
number = tk.StringVar()
number_chosen = ttk.Combobox(win, width=12, textvariable=number, state=‘readonly’)
number_chosen[‘values’] = (1, 2, 4, 42, 100)
number_chosen.grid(column=1, row=1)
number_chosen.current(0)

chVarDis = tk.IntVar()
check1 = tk.Checkbutton(win, text=“Disabled”, variable=chVarDis, state=‘disabled’)
check1.select()
check1.grid(column=0, row=4, sticky=tk.W)

chVarUn = tk.IntVar()
check2 = tk.Checkbutton(win, text=“UnChecked”, variable=chVarUn)
check2.deselect()
check2.grid(column=1, row=4, sticky=tk.W)

chVarEn = tk.IntVar()
check3 = tk.Checkbutton(win, text=“Enabled”, variable=chVarEn)
check3.deselect()
check3.grid(column=2, row=4, sticky=tk.W)

GUI Callback function

def checkCallback(*ignoredArgs):
# only enable one checkbutton
if chVarUn.get(): check3.configure(state=‘disabled’)
else: check3.configure(state=‘normal’)
if chVarEn.get(): check2.configure(state=‘disabled’)
else: check2.configure(state=‘normal’)

trace the state of the two checkbuttons

chVarUn.trace(‘w’, lambda unused0, unused1, unused2 : checkCallback())
chVarEn.trace(‘w’, lambda unused0, unused1, unused2 : checkCallback())

Radiobutton Globals

COLOR1 = “Blue”
COLOR2 = “Gold”
COLOR3 = “Red”

Radiobutton Callback

def radCall():
radSel=radVar.get()
if radSel == 1: win.configure(background=COLOR1)
elif radSel == 2: win.configure(background=COLOR2)
elif radSel == 3: win.configure(background=COLOR3)

create three Radiobuttons using one variable

radVar = tk.IntVar()

rad1 = tk.Radiobutton(win, text=COLOR1, variable=radVar, value=1, command=radCall)
rad1.grid(column=0, row=5, sticky=tk.W, columnspan=3)

rad2 = tk.Radiobutton(win, text=COLOR2, variable=radVar, value=2, command=radCall)
rad2.grid(column=1, row=5, sticky=tk.W, columnspan=3)

rad3 = tk.Radiobutton(win, text=COLOR3, variable=radVar, value=3, command=radCall)
rad3.grid(column=2, row=5, sticky=tk.W, columnspan=3)

Using a scrolled Text control

scrol_w = 30
scrol_h = 3
scr = scrolledtext.ScrolledText(win, width=scrol_w, height=scrol_h, wrap=tk.WORD)
scr.grid(column=0, columnspan=3)

name_entered.focus() # Place cursor into name Entry
#======================

Start GUI

#======================
win.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值