第一个GUI程序

Python GUI 开发有好几个第三方的库,我选择的是tkinter

最简单的一个GUI程序

import tkinter as tk//给库来个简写,用的时候简洁一点
root = tk.Tk()//初始化一个根窗口
root.mainloop()//放入主运行循环
class Tk(Misc, Wm):
    """Toplevel widget of Tk which represents mostly the main window
    of an application. It has an associated Tcl interpreter."""

//Tk是一个类,代表程序的主窗口(我觉得用窗口比较好理解,也可以理解为控件),根窗口

一个程序只能创建一个根窗口,并且需要在所有其他控件之前创建.
最后,只有将窗口放到主运行循环中,窗口才会显示.

clipboard.png

有了主窗口,接着我们可以往上面添加其他控件,

添加一个Hello,world的label

和iOS开发差不多,有三步

  1. 创建控件
  2. 设置属性
  3. 添加控件

在初始化根窗口后添加如下代码,并运行

helloworld_label = tk.Label(root,text='hello,world')//创建label并设置属性
helloworld_label.pack()//把控件包装到父控件中

图片描述

拉大根窗口
图片描述

label中没有pack方法,向上查找Widget中也没有,但是Widget继承自Pack,Pack对象中有一个属性pack,对应了pack_configure方法

     pack = configure = config = pack_configure
     
     ...
     
     Base class to use the methods pack_* in every widget."""
     def pack_configure(self, cnf={}, **kw):
         """Pack a widget in the parent widget. Use as options:
           包装一个控件到父控件中,使用下面的选项
         after=widget - pack it after you have packed widget:在包装某个控件之后包装
         anchor=NSEW (or subset) - position widget according to
                                   given direction:根据给定方向摆放
         before=widget - pack it before you will pack widget:在包装某个控件之前包装
         expand=bool - expand widget if parent size grows:当父控件尺寸变化,更改其大小
         fill=NONE or X or Y or BOTH - fill widget if widget grows
         in=master - use master to contain this widget
         in_=master - see 'in' option description
         ipadx=amount - add internal padding in x direction
         ipady=amount - add internal padding in y direction
         padx=amount - add padding in x direction
         pady=amount - add padding in y direction
         side=TOP or BOTTOM or LEFT or RIGHT -  where to add this widget.
         """
         self.tk.call(
               ('pack', 'configure', self._w)
               + self._options(cnf, kw))

通过参数可以对子控件进行相关配置
如pack(side='bottom'),拉大窗口,文字仍旧居于底部
图片描述

注意

label控件不同于iOS里面的label,此处,除了用于展示文本,还可以用于展示图片

按钮的使用

学习控件都有固定的模板,无非是创建,设置属性(包括布局),添加.接着我们一起探索一下按钮的使用.
点击按钮将label文字改为hello,python

在label包装代码后面添加如下代码

change_button = tk.Button(root,text='click me',command=change_word)
change_button.pack(side='top')

...

def change_word():
    change_button.setvar('hello,python')

按照我的推测,应该拿到label,在执行set方法赋值,结果竟然....不行.参考了下别人.
需要用tk中的StringVar存储字符串变量,以及一开始设置的时候不用text属性而用textvariable

修改

def change_word():
    var.set('hello,python')

... 

var = tk.StringVar(value='hello,world')
helloworld_label = tk.Label(root,textvariable=var)
注意

函数定义如果放在下面会找不到方法,需要放在用到之前.

我感到有些郁闷,自己探索该怎么发现会用到tk中的StringVar呢?不过很快我就释然了,这些基本用法,文档都应该写清楚的,知道怎么用就好了,不必深究

设置根窗口大小

root.geometry('300x200')

我觉得*更合理,没想到真的是x

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值