第一章代码重构

代码重构方便之后扩展(主要是循环生成Radiobuttion那里)

# -*- coding: utf-8 -*-

# import
import tkinter as tk  # 1 imports
from tkinter import ttk

from tkinter import scrolledtext as st

win = tk.Tk()  # 2 Create instance
win.title("Python GUI")  # 3 Add a title
# win.resizable(0, 0)           # 4 Disable resizing the GUI

# add a label                   #4
aLabel = ttk.Label(win, text="输入文本:")
aLabel.grid(column=0, row=0)  # 5

ttk.Label(win, text="choose a number").grid(column=1, row=0)
number = tk.StringVar()

# only be able to select the values we have programmed into the Combobox:state="readonly"
numberChosen = ttk.Combobox(win, width=12, textvariable=number, state="readonly")
numberChosen.grid(column=1, row=1)
numberChosen["values"] = (1, 2, 3, 4, 5, 6, 12)
numberChosen.current(3)


def clickMe():
    action.configure(text="hello " + name.get() + "-" + number.get())
    # aLabel.configure(foreground="red")


# add a button                   #4
action = ttk.Button(win, text="点我", command=clickMe)
action.grid(column=3, row=1)
# action.configure(state="disabled")  # Disable the Button Widget

# Adding a Textbox Entry widget    # 5
name = tk.StringVar()
nameEntered = ttk.Entry(win, width=12, textvariable=name)
nameEntered.grid(column=0, row=1)
nameEntered.focus()  # Place cursor into name Entry

# Creating three checkbuttons    # 1
# 0 (unchecked) or 1 (checked) so the type of the variable is a tkinter integer.
chVarDis = tk.IntVar()  # 2
check1 = tk.Checkbutton(win, text="Disabled", variable=chVarDis, state='disabled')  # 3
check1.select()  # 4
check1.grid(column=0, row=4, sticky=tk.W)  # 5

chVarUn = tk.IntVar()  # 6
check2 = tk.Checkbutton(win, text="UnChecked", variable=chVarUn)
check2.deselect()  # 8
check2.grid(column=1, row=4, sticky=tk.W)  # 9

chVarEn = tk.IntVar()  # 10
check3 = tk.Checkbutton(win, text="Enabled", variable=chVarEn)
check3.select()  # 12
check3.grid(column=2, row=4, sticky=tk.W)  # 13

# Radiobutton Globals #1 
# COLOR1 = "DarkSalmon" #2
# COLOR2 = "honeydew" #3
# COLOR3 = "AliceBlue" #4
# Radiobutton Callback # 5
# create three Radiobuttons using one variable(绑定到一个变量上可以在单选按钮中只选中一个)
# radVar = tk.IntVar()
# def radCall(): #6
#       radSel=radVar.get()
#       if   radSel == 1: win.configure(background=COLOR1)
#       elif radSel == 2: win.configure(background=COLOR2)
#       elif radSel == 3: win.configure(background=COLOR3)
#
# rad1 = tk.Radiobutton(win, text=COLOR1, variable=radVar, value=1, command=radCall)              # 9
# rad1.grid(column=0, row=5, sticky=tk.W)  # 10
# rad2 = tk.Radiobutton(win, text=COLOR2, variable=radVar, value=2, command=radCall)                             # 11
# rad2.grid(column=1, row=5, sticky=tk.W)  # 12
# rad3 = tk.Radiobutton(win, text=COLOR3, variable=radVar, value=3, command=radCall)                             # 13
# rad3.grid(column=2, row=5, sticky=tk.W)  # 14


# 代码重构(refactor our code)
# First, we change our Radiobutton global variables into a list.
colors = ["DarkSalmon", "honeydew", "AliceBlue"]
# create three Radiobuttons using one variable
radVar = tk.IntVar()
# Next we are selecting a non-existing index value for radVar.
# (如果不设置为range范围外的值,初始化页面默认会选中第一个并且不会触发变更背景色的回调函数)
radVar.set(99)
# We have also changed the callback function to be zero-based, using the list instead of module-level global variables.
# Radiobutton callback function
def radCall():
    radSel = radVar.get()
    if radSel == 0:
        win.configure(background=colors[0])
    elif radSel == 1:
        win.configure(background=colors[1])
    elif radSel == 2:
        win.configure(background=colors[2])
# Now we are creating all three Radiobutton widgets within one loop.
for col in range(3):
    curRad = 'rad' + str(col)
    curRad = tk.Radiobutton(win, text=colors[col], variable=radVar, value=col, command=radCall)
    curRad.grid(column=col, row=5, sticky=tk.W)

# Using a scrolled Text control
scrollW = 30
scrollH = 3
scroll = st.ScrolledText(win, width=scrollW, height=scrollH, wrap=tk.WORD)
scroll.grid(column=0, columnspan=3)
scroll.focus()

win.mainloop()  # 5 Start GUI

运行效果图

Paste_Image.png

更多颜色参考:http://www.tcl.tk/man/tcl8.5/TkCmd/colors.htm

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,355评论 19 139
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 8,455评论 0 4
  • 第一、在简书列出自己2017年的梦想清单,对应下面的每一个栏目写三个目标: 栏目 目标1 ...
    想飞的波米诺pomelo阅读 1,280评论 0 0
  • 采桑子.思我主 初秋思人难思量,烟障重雾,雨住几度,只堪梦里寻归途。 倦衣凭窗怯远望,风疏江舞,繁锦映目,犹见双栖...
    风筝神阅读 2,790评论 0 1
  • 舆论:故事性的阐述会留存更久。 戊戌变法:围园''后,袁世凯的背叛,戊戌六君子 真正的戊戌变法真相是什么?最根本的...
    小樱桃没有丸子阅读 1,885评论 0 0