Python GUI Programming Cookbook -自学笔记

        基础知识点过完后,我一心想做个东西出来,就开始自学这本书,就是全英文有点烦。。

2017版​​​​
#-*- coding: utf-8 -*
#======================
#imports
#======================
import tkinter as tk
from tkinter import ttk # ttk比tk功能和美观方面更强大。
from tkinter import scrolledtext #从tkinter模块导入scrolledtext类(输入滚动文本框)
# Create instane

win = tk.Tk()  #导入tk库中Tk类构造实例win,最底层容器,之后label和button要嵌入到其上。

#Add a title
win.title('Note book')

#Adding a Label that will get modified
#将win这个父类容器传递给标签label,之后label会显示在win窗口上。
#a_label = ttk.Label(win, text = 'This is my note book!')
#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)
ttk.Label(win,text = 'Choose a number! ').grid(column = 1,row = 0)
#Adding a Text box entry widget
name = tk.StringVar()  #声明tkinter变量
name_entered = ttk.Entry(win,width = 12, textvariable = name) #Entry是一个文本输入控件
name_entered.grid(column = 0,row = 1)
name_entered.focus()     # 放置一个光标(cursor)在文本控件输入框

number = tk.StringVar()
number_chosen = ttk.Combobox(win,width = 12, textvariable = number, state = 'readonly')
number_chosen['values'] = (1,2,4,6,10,12)  #虽然未在元组加‘’,但由于之前定义为StringVar(),故为字符串
number_chosen.grid(column = 1,row = 1)
number_chosen.current(0)

#Adding a Button
action = ttk.Button(win, text = 'Click Me', command = click_me)
action.grid(column = 2,row = 1)
#action.configure(state = 'disable')  #禁用button按钮

#Creating three checkbuttons
#创建了三个按钮部件
chVarDis = tk.IntVar()
check1 = tk.Checkbutton(win,text = 'Disable', variable = chVarDis, state = 'disable')
check1.select()
check1.grid(column = 0,row = 4, sticky = tk.W)  #利用sticky属性使得这些按钮始终与网格左对齐

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

chVarEn = tk.IntVar()
check3 =  tk.Checkbutton(win,text = 'Enable', variable = chVarEn)
check3.select()
check3.grid(column = 2,row = 4, sticky = tk.W)


#Radiobutton Globals
COLOR1 = 'Blue'
COLOR2 = 'Gold'
COLOR3 = 'Red'
#Radiobutton callbak
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)

#Creat 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 = 40
scrol_h = 3
scr = scrolledtext.ScrolledText(win, width=scrol_w, height=scrol_h, wrap=tk.WORD)
scr.grid(column = 0,columnspan = 3)
#=====================
#Start
#=====================
win.mainloop()

第一章,主要代码为以上,预期效果为下:

总结:主要介绍了几个widget的使用,依次在win窗口嵌入这些部件。

有label,buttton,entry,设置光标,combox,chekbutton,radiobutton,scrolledtext。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值