Chapter 1: Creating the GUI Form and Adding Widgets

# -*- 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

# 代码重构(refactor our code)
# Radiobutton Globals #1 更多颜色参考:http://www.tcl.tk/man/tcl8.5/TkCmd/colors.htm
COLOR1 = "DarkSalmon" #2
COLOR2 = "honeydew" #3
COLOR3 = "AliceBlue" #4
# Radiobutton Callback # 5
# colors = ["DarkSalmon", "honeydew", "AliceBlue"]
# 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

# 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
时间: 2024-09-27 16:58:39

Chapter 1: Creating the GUI Form and Adding Widgets的相关文章

[引用] Eclipse Forms Programming Guide

guid Eclipse Forms Programming GuideInitial creation: 21 February 2004Added table wrap sample: 22 February 2004IntroductionThis document has been written to help you use the new Eclipse 3.0 feature called 'Eclipse Forms'. The content will eventually

Table of Contents

Copyright      About the Authors      Acknowledgments      Introduction         What Is the Purpose of This Book?         Who Should Read This Book?         Prerequisites          Part I.  Introduction to Microsoft .NET and ASP.NET         Chapter 1.

Awesome Python

    Awesome Python      A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome-php. Awesome Python Environment Management Package Management Package Repositories Distribution Build Tools Interactive Interp

C++ 100款开源界面库 (10)

(声明:Alberl以后说到开源库,一般都是指著名的.或者不著名但维护至少3年以上的.那些把代码一扔就没下文的,Alberl不称之为开源库,只称为开源代码.这里并不是贬低,像Alberl前面那个系列的教程<2013 duilib入门简明教程>,还有本系列教程,还有前面介绍的CodeProject,基本上都是代码往上面一扔,就不用再怎么维护的.这些都称之为开源代码,其实开源代码对新手的帮助更大,因为很简明的说明了代码用法~O(∩_∩)O~)       前面两个教程已经对制作界面的几种方式进行了

Bonita Open Solution v5.4.1发布 协同工作流系统

Bonita 是一个符合WfMC规范.灵活的协同工作流系统. 对于各种动作如流程概念建模.定义.实例化.流程控制和用户交互等提供了全面的集成图形工具. 特点: 1. 100% 基于浏览器.使用SOAP和XML数据绑定技术的Web Services封装了已有的工作流业务方法并将它们以基于J2EE的Web Service形式发布. 2. 基于活动预测模型的第三代工作流引擎. 更新日志: - 0003294: [studio-integration] when deploying business a

PS网页设计教程XXII——在PS中创建单页复古网页布局

作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,"熟读唐诗三百首,不会作诗也会吟". 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试翻译这些优秀的教程.因为翻译能力有限,翻译的细节上还有待推敲,希望广大网友不吝赐教. 约定: 1.本文的软件是Photoshop CS5版本 2.原教程的截图是英文的,本人在重新制作的基础上,重新截了中文版的图 3.原文中有些操作没有给出参数.本人在反复测试的情况下测定了一些参数,以红色的

PS网页设计教程XXI——在Photoshop中创建一个光质感网页设计

作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,"熟读唐诗三百首,不会作诗也会吟". 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试翻译这些优秀的教程.因为翻译能力有限,翻译的细节上还有待推敲,希望广大网友不吝赐教. 约定: 1.本文的软件是Photoshop CS5版本 2.原教程的截图是英文的,本人在重新制作的基础上,重新截了中文版的图 3.原文中有些操作没有给出参数.本人在反复测试的情况下测定了一些参数,以红色的

Machine and Deep Learning with Python

Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstitions cheat sheet Introduction to Deep Learning with Python How to implement a neural network How to build and run your first deep learning network Neur

Devexpress VCL Build v2013 vol 13.2.2 发布

devexpress 2013 的第二个大版本出来了,一如既往, 基本上还是一个大补丁包.各位看官,自己看.   What's New in 13.2.2 (VCL Product Line)   New Major Features in 13.2 What's New in VCL Products 13.2 Breaking Changes To learn about breaking changes in this version, please refer to the follow