tkinter related notes

Preface

GUI is very important in previous and future work. At the beginning of the winter vacation, while running the model (dry running...hard running...), I irresponsibly throw it aside and start to get started with tkinter.
First of all, I am going to watch the video of station b, and I found that Mo Fan is also more relevant . Let’s make some relevant notes first.

Getting started tutorial notes

import

import tkinter as tk

window

Everything is based on windows.

Define the window:

window = tk.Tk() # create window

Change the window name:

window.title('my window') # change title

Change the size of the window

window.geometry('1000x200') # size A(width)xB(height)

where

Used to dynamically change the text

var=tk.StringVar()

label

A thing that displays text

Define label

l = tk.Label(window,
             text = 'hello my world', # 显示该text
             textvariable = var, # 显示该var对应的文本,而var是可以变化的
             bg = 'green',  # background color
             font=('Arial', 12),
             width = 15,
             height = 2) 
  • If we want to change the text displayed by the label, we can change var. Generally: 1. Var may be obtained from other components by get(). For example, there is an entity component described below. Its function is to define an input box. . Then after we input, we can change var by var = entity.get(). 2. Var may be set() artificially. For example, there is a button component to be mentioned below. Clicking on it will trigger the command in the function once, then we can set the var artificially in the command. (Two examples will be given below)
  • In fact, text is not fixed, it can be modified by label.confit(text=...)

button

That is, the button, every time it is clicked, it will trigger an internally defined command

Define button

b = tk.Button(window,
              text='hit me', # 按钮上的文本
              width=15,
              height=2,
              command = hit_me,# a function which will run when you hit the botton
              ) # create a botton on window

Define command(hit_me)

on_hit = False # hit flag 
def hit_me():
    global on_hit
    if on_hit == False:
        on_hit = True
        var.set("you hit me") # 这里就是上面说的改变var的第二种方式(var.set())
    else:
        on_hit = False
        var.set("")

radiobutton

Just like a button, once clicked, besides the command function, you can also change the function of var (variable=…, value=…)

r1 = tk.Radiobutton(
    master=window,
    text='Option A', # 该button上面的文本
     ###### if you choose Option A,var will be 'A'
    variable=var, # 需要被修改的var
    value='A', # 修改成什么
    ######
    command=print_selection
)

text

It’s also a place to display text, but it’s not the same as label. It’s more flexible.

Define text

t = tk.Text(
    master=window,
    height=2
)

Insert text at the cursor position

t.insert('insert',var) # "insert"代表以插入到光标位置的方式插入, var上面介绍过

Insert text to the end

t.insert('end',var)

Insert text into specified row and specified column

def insert_sp():
    var = e.get()  # get text from Entry
    t.insert(1.1 ,var)

entity

Similar to text, understand it as an input box in the general login interface!

Define entity

e = tk.Entry(master=window,
             show = "*" # \ '*'
             )

Here show="*", when used to enter the password, all asterisks are displayed to the user.

The first way to change var (get()): input text into the entity box, and then entity.get() changes var

var = entity.get() 

Listbox

给定一连串的选项,然后用光标去选择。

Define Listbox

var2 = tk.StringVar()
var2.set((11,22,33,44))  # 先初始化部分选项
lb = tk.Listbox(
    master = window,
    listvariable = var2
)

Update Listbox option-insert

list_items = [1,2,3,4]
for item in list_items:
    lb.insert('end', item) # end:末尾
lb.insert(1,'hello') # 1 第一行
lb.insert(2,'world')

Update the option of Listbox-delete

lb.delete(2)

Get which one of the options provided by Listbox is selected by the user

value = lb.get(Listbox.curselection())  # 光标位置

scale

Slider

Define scale

s = tk.Scale(
    master=window,
    label='try me', # title
    from_ = 5, # from...to...
    to = 10,
    orient = tk.HORIZONTAL,
    length = 200,
    showvalue = 0, # default = 0
    tickinterval = 1, # interval
    resolution = 1, # 0.01 两位小数; 1 整数
    command = print_selection # 默认有传入值:selected value
)

Guess you like

Origin blog.csdn.net/jokerxsy/article/details/112675402