Chapter 2: Layout Management(一)

scroll.grid(column=0, columnspan=3, sticky='WE') # sticky='WE'

(WEST,EAST)该属性左右对其,做下面的测试时可以注释查看效果

在frame中嵌入frame以调整样式,在sticky中加入约束,EWSN东西南北对其也可以使用tk.W等代替

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

# We are creating a container frame to hold all other widgets
monty = ttk.LabelFrame(win, text=' Monty Python')
# monty = ttk.LabelFrame(win, )
monty.grid(column=0, row=0)

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

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

# only be able to select the values we have programmed into the Combobox:state="readonly"
numberChosen = ttk.Combobox(monty, width=12, textvariable=number, state="readonly")
numberChosen.grid(column=1, row=1, sticky=tk.W)
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(monty, text="点我", command=clickMe)
action.grid(column=2, row=1)
# action.configure(state="disabled")  # Disable the Button Widget

# Adding a Textbox Entry widget    # 5
name = tk.StringVar()
nameEntered = ttk.Entry(monty, width=12, textvariable=name)
nameEntered.grid(column=0, row=1, sticky=tk.W)
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(monty, 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(monty, text="UnChecked", variable=chVarUn)
check2.deselect()  # 8
check2.grid(column=1, row=4, sticky=tk.W)  # 9

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

tk.Scrollbar()

# 代码重构(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()
print(radVar)
# 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(monty, 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(monty, width=scrollW, height=scrollH, wrap=tk.WORD)
scroll.grid(column=0, columnspan=3, sticky='WE') # sticky='WE' 该属性左右对其,做下面的测试时可以注释查看效果
# scroll.grid(column=0, columnspan=3)

# Create a container to hold labels(label的长度取决于LabelFrame标题的长度,当添加的LabelFrame组件的长度大于硬编码的组件大小时,
# 我们会自动将这些组件移动到column 0 列的中心,并在组件左右两侧填充空白,具体可以参看下列两行的区别)
labelsFrame = ttk.LabelFrame(monty, text=' Labels in a Frame ')
# labelsFrame = ttk.LabelFrame(win)
# labelsFrame.grid(column=0, row=7, padx=20, pady=40)
labelsFrame.grid(column=0, row=7)

# Place labels into the container element # 2
ttk.Label(labelsFrame, text='Label 1').grid(column=0, row=0)
ttk.Label(labelsFrame, text='Label 2').grid(column=0, row=1)
ttk.Label(labelsFrame, text='Label 3').grid(column=0, row=2)
# Place cursor into name Entry
nameEntered.focus()
#
# for child in labelsFrame.winfo_children():
#     child.grid_configure(padx=8, pady=4)

win.mainloop()  # 5 Start GUI

最终效果图:

Paste_Image.png

时间: 2024-11-08 17:30:11

Chapter 2: Layout Management(一)的相关文章

ZetCode PyQt4 tutorial layout management

!/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This example shows three labels on a window using absolute positioning. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from P

Spring Boot 项目构建 之 安装 Gradle(Chapter 4. Installing Gradle)

白话解析如下: =========== Mac 系统下载到 Gradle 压缩包后,解压到某一目录下,然后设置环境变量,命令如下: vi ~/.profile 按 i 进行编辑模式,用方向键向下移动光标到末尾,并添加如下内容: export GRADLE_HOME=/xxx/yyy/.../gradle-2.5 export PATH=$GRADLE_HOME/bin:$PATH 按 ESC 键退出编辑模式,输入 w 回车保存,再输入 q 回车退出 vi 编辑器.接下来测试 gradle 是否可

Gradle cookbook(转)

build.gradle apply plugin:"java" [compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8' sourceCompatibility = '1.6' targetCompatibility = '1.6' buildDir = "target" version = '1.0' sourceSets{ main{ java{ srcDirs 'src' } } }

J2EE Enterprise Beans(原文)

enterprise|j2ee Enterprise beans are the J2EE components that implement Enterprise JavaBeans (EJB) technology. Enterprise beans run in the EJB container, a runtime environment within the J2EE server (see Figure 1-5). Although transparent to the appli

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.

聊聊我对Java内存模型的理解

所有的编程语言中都有内存模型这个概念,区别于微架构的内存模型,高级语言的内存模型包括了编译器和微架构两部分.我试图了解了Java.C#和Go语言的内存模型,发现内容基本大同小异,只是这些语言在具体实现的时候略有不同. 我们来看看Java内存模型吧,提到Java内存模型大家对这个图一定非常熟悉: 这张图告诉我们在线程运行的时候有一个内存专用的一小块内存,当Java程序会将变量同步到线程所在的内存,这时候会操作工作内存中的变量,而线程中变量的值何时同步回主内存是不可预期的.但同时Java内存模型又告

@AspectJ support (good)

AspectJ类型匹配的通配符:*:匹配任何数量字符:..:匹配任何数量字符的重复,如在类型模式中匹配任何数量子包:而在方法参数模式中匹配任何数量参数.+:匹配指定类型的子类型:仅能作为后缀放在类型模式后边.java.lang.String 匹配String类型:java.*.String 匹配java包下的任何"一级子包"下的String类型://*只能匹配一个子包如匹配java.lang.String,但不匹配java.lang.ss.Stringjava..* 匹配java包及任

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

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

std::string的Copy-on-Write:不如想象中美好

Copy-on-write(以下简称COW)是一种很重要的优化手段.它的核心思想是懒惰处理多个实体的资源请求,在多个实体之间共享某些资源,直到有实体需要对资源进行修改时,才真正为该实体分配私有的资源. COW技术的一个经典应用在于Linux内核在进程fork时对进程地址空间的处理.由于fork产生的子进程需要一份和父进程内容相同但完全独立的地址空间,一种做法是将父进程的地址空间完全复制一份,另一种做法是将父进程地址空间中的页面标记为"共享的"(引用计数+1),使子进程与父进程共享地址空