Chapter 1: Creating the GUI Form and Adding Widgets

简介: # -*- coding: utf-8 -*-# importimport tkinter as tk # 1 importsfrom tkinter import ttkfrom tkinter import scrolledtext as stwin = tk.
# -*- 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
目录
相关文章
|
存储 算法 数据挖掘
【2023年中国高校大数据挑战赛 】赛题 B DNA 存储中的序列聚类与比对 Python实现
本文介绍了2023年中国高校大数据挑战赛赛题B的Python实现方法,该赛题涉及DNA存储技术中的序列聚类与比对问题,包括错误率分析、序列聚类、拷贝数分布图的绘制以及比对模型的开发。
423 2
【2023年中国高校大数据挑战赛 】赛题 B DNA 存储中的序列聚类与比对 Python实现
|
12月前
|
数据采集 监控 大数据
大数据项目管理:从规划到执行的全景指南
大数据项目管理:从规划到执行的全景指南
216 4
|
机器学习/深度学习 存储 人工智能
AI与量子计算:推动计算科学的边界
【10月更文挑战第7天】AI与量子计算的融合,标志着计算科学进入了一个全新的时代。在这个时代里,计算能力的边界被不断拓宽,科技创新的速度不断加快。我们有理由相信,在未来的日子里,AI与量子计算将继续携手并进,共同推动计算科学向着更加智能、更加高效的方向发展。让我们期待这一天的到来,共同见证计算科学的无限可能。
|
SQL Java 数据库连接
Spring问题之@RequestMapping注解的作用和使用方式是啥
Spring问题之@RequestMapping注解的作用和使用方式是啥
269 3
|
存储 供应链 监控
Python 实战:打造智能进销存系统
Python 实战:打造智能进销存系统
911 0
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp微信小程序的图书管理系统的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的图书管理系统的详细设计和实现
228 2
|
NoSQL Redis Windows
windows环境启动redis-server.exe出现闪退问题解决方案(亲测有效)
windows环境启动redis-server.exe出现闪退问题解决方案(亲测有效)
2393 0
|
测试技术 数据安全/隐私保护 Java
基于SpringBoot+Vue+uniapp的金融支付终端管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的金融支付终端管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
205 6
|
存储 Kubernetes 安全
如何与不同节点共享 Docker 容器
【8月更文挑战第27天】
489 5
|
存储 SQL 数据库
安卓移动应用开发(安卓系统开发教程)
安卓移动应用开发(安卓系统开发教程)

热门文章

最新文章