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。

已标记关键词 清除标记
相关推荐
Title: Python GUI Programming Cookbook 2nd Author(s): Burkhard A. Meier Publisher: Packt Publishing Year: 2017 Edition: 2nd Language: English Pages (biblio\tech): 422\0 ISBN: 1787129454, 9781787129450 Key FeaturesUse object-oriented programming to develop amazing GUIs in PythonCreate a working GUI project as a central resource for developing your Python GUIsEasy-to-follow recipes to help you develop code using the latest released version of PythonBook Description Python is a multi-domain, interpreted programming language. It is a widely used general-purpose, high-level programming language. It is often used as a scripting language because of its forgiving syntax and compatibility with a wide variety of different eco-systems. Python GUI Programming Cookbook follows a task-based approach to help you create beautiful and very effective GUIs with the least amount of code necessary. This book will guide you through the very basics of creating a fully functional GUI in Python with only a few lines of code. Each and every recipe adds more widgets to the GUIs we are creating. While the cookbook recipes all stand on their own, there is a common theme running through all of them. As our GUIs keep expanding, using more and more widgets, we start to talk to networks, databases, and graphical libraries that greatly enhance our GUI's functionality. This book is what you need to expand your knowledge on the subject of GUIs, and make sure you're not missing out in the long run. What you will learnCreate the GUI Form and add widgetsArrange the widgets using layout managersUse object-oriented programming to create GUIsCreate Matplotlib chartsUse threads and talking to networksTalk to a MySQL database via the GUIPerform unit-testing and internationalizing the GUIExtend the GUI with third-party graphical librariesGet to know the best practices to create GUIsAbout the Author Burkhard A. Meier has more than 17 years of professional experience working in the software industry as
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:白松林 返回首页