tkinter variable category of Python

Article directory

1, Basic introduction of variable categories

Some controls will change content when they are executed, for example, text box (Entry), radio button (Radio button), etc. the option button is to select a button, select multiple radio, etc

Some controls we can change their content, such as label. If we want to change their content, we can use the parameters of these controls, such as textvariable, variable, onvalue, etc

However, to process the parameters of a control as variables, you need to use the variable classes in the tkinter module.

This category has four subcategories, each of which is actually a construction method of data type. We can combine them with control related parameters through the data types of these four subcategories

  • x = IntVar(): integer variable, default is 0
  • x = DoubleVar(): floating point variable, default is 0.0
  • x = StringVar(): string variable, default is' '
  • X = Boolean var(): boolean variable, True is 1, False is 0

2, set() method

Using the set() method to set variable contents

import tkinter

# Switch for control display
msg = False
def click():
    # Change variables
    global msg
    if msg ==False:
        msg = True
        # Set text
        x.set("Python!!!")
    else:
        msg = False
        # Set space
        x.set("")

root = tkinter.Tk()
x = tkinter.StringVar()
# textvariable is variable, and will automatically change content as string changes
label = tkinter.Label(root, textvariable=x, bg="lightyellow", fg="red", font="Verdana 18 bold", width=25, height=2)
label.pack()

button = tkinter.Button(root, text="Please click", command=click)
button.pack()

root.mainloop()

Operation result:

3, get() method

Using get() method to get variable content

We can use the get() method to improve this little program

import tkinter

def click():
    if x.get() == "":
        x.set("Python!!!")
    else:
        x.set("")

root = tkinter.Tk()
x = tkinter.StringVar()
# textvariable is variable, and will automatically change content as string changes
label = tkinter.Label(root, textvariable=x, bg="lightyellow", fg="red", font="Verdana 18 bold", width=15, height=2)
label.pack()

button = tkinter.Button(root, text="Please click", command=click)
button.pack()

root.mainloop()

Operation result:

4, trace() use mode w

We can use variable settings to track Widget controls and let the program automatically execute functions when their contents change

Example:

import tkinter

# Here we have to add * args. Let's talk about it in detail
def show(*args):
    print("Data:", string.get())

root = tkinter.Tk()
string = tkinter.StringVar()
entry = tkinter.Entry(root, textvariable=string)
entry.pack(padx=5, pady=10)
string.trace("w", show)

root.mainloop()

Operation result:

string.trace("w", show)

  • The first parameter is mode. w means that the show function will be executed automatically when a write is executed
  • You can also take the function name by yourself. This action is called change tracking.

We can use set and get methods to realize synchronous display in windows

Example:

import tkinter

def show(*args):
    stringLabel.set(stringEntry.get())
    print("Data:", stringEntry.get())

root = tkinter.Tk()
stringEntry = tkinter.StringVar()
entry = tkinter.Entry(root, textvariable=stringEntry)
entry.pack(padx=5, pady=10)
stringEntry.trace("w", show)

stringLabel = tkinter.StringVar()
label = tkinter.Label(root, textvariable=stringLabel)
stringLabel.set("Synchronous display")
label.pack(padx=5, pady=10)


root.mainloop()

Operation result:

5, trace() usage mode r

We can also design to trace and execute specific functions when the content of the control is read

Example:

import tkinter

def show(*args):
    Tr.set(Tw.get())

def getting(*args):
    print("Warning!! Reading data")

def hit():
    print("The data are:", Tw.get())

root = tkinter.Tk()
Tw = tkinter.StringVar()
entry = tkinter.Entry(root, textvariable=Tw)
entry.pack(padx=5, pady=10)
Tw.trace("w", show)
Tw.trace("r", getting)

Tr = tkinter.StringVar()
label = tkinter.Label(root, textvariable=Tr)
Tr.set("Synchronous display")
label.pack(padx=5, pady=10)

button = tkinter.Button(root, text="Click read", command=hit)
button.pack(padx=5, pady=10)

root.mainloop()

Operation result:

Tw.trace("r", getting)

  • The first parameter is pattern. r means to execute the getting function automatically when there is a read
  • You can also take the function name yourself. This action is called read trace.

6, Method parameters of trace() method call

def show(*args):

*args actually passes three parameters: tk variable name, index index index and mode mode

However, at present, the actual support for tk variable name and index index has not been completed. The third parameter can list r or w mode

Since the program we designed does not need to pass parameters, we can directly use * args as the parameter content

Example:

import tkinter

def show(name, index, mode):
    stringLabel.set(stringEntry.get())
    print("name = %r, index = %r, mode = %r" % (name, index, mode))

root = tkinter.Tk()
stringEntry = tkinter.StringVar()
entry = tkinter.Entry(root, textvariable=stringEntry)
entry.pack(padx=5, pady=10)
stringEntry.trace("w", show)

stringLabel = tkinter.StringVar()
label = tkinter.Label(root, textvariable=stringLabel)
stringLabel.set("Synchronous display")
label.pack(padx=5, pady=10)


root.mainloop()

Operation result:

Here's an explanation

%r

%s is to pass the variable to the str() function, and the result is to convert the variable into a format suitable for human reading

%r is to insert variables into the repr() function. The result is to convert variables into a format suitable for machine reading. The variables after% r can be understood as an object

Thank you for watching. I will keep updating. If there are any mistakes or suggestions, please contact me personally

31 original articles published, 36 praised, 3987 visited
Private letter follow

Keywords: Python Windows

Added by N350CA on Wed, 11 Mar 2020 07:56:50 +0200