Python Basics-gui Programming-tk-stringvar

Source: Internet
Author: User

1, how to elicit Stringvar

It was previously thought that Stringvar was a Java-like object variable of type string, and today, when you want to set the value of the Stringvar variable, the search finds that stringvar is not a python built-in object, but a Tkinter object. This aroused my interest, I feel the need for targeted learning

2, the role of Stringvar

Querying a lot of data, when we use the interface programming, sometimes we need to track the variable value changes, to ensure that the value of changes can be displayed at any time on the interface. Because Python cannot do this, it uses the corresponding object of Tcl, that is, Stringvar, Booleanvar, Doublevar, Intvar need to play a role

Here is an example, after clicking the button, the ListBox option becomes 4 items, one more Java option:


Def changeitems ():
Print Cnames.get ()
tnames = ' python ', ' TCL ', ' Ruby ', ' Java '
Cnames.set (Tnames)

root = tkinter.tk ()
Root.geometry (' +400+200 ')
Root.minsize (400,200)
Root.title ("Test")

tnames = ' python ', ' TCL ', ' Ruby '
CNAMEs = Stringvar ()
Cnames.set (Tnames)
L = Listbox (root, listvariable = Cnames,height = ten). Grid ()

Ttk. button (Root,text = "submit", command = changeitems). Grid ()

Root.mainloop ()

3. Interesting places

The example above is actually a change to the example in the following link
Http://www.tkdocs.com/tutorial/morewidgets.html

In this example, the assignment to the Stringvar variable uses the tuples type, and the problem is that the type cannot be modified, which brings up the question of how the variable is modified.

So I changed the program, the initial assignment was a list, and the result came up with something that surprised me:

Tnames = [' Python ', ' TCL ', ' Ruby ']
CNAMEs = Stringvar ()
Cnames.set (Tnames)
L = Listbox (root, listvariable = Cnames,height = ten). Grid ()
The results of this program execution, the first of the list shown in the listbox is [' Python ', and the second is ' TCL ', the third is ' Ruby ']
The weird thing about this is that it really breaks down into three paragraphs, but three paragraphs contain the [], string ', and split, which are required by the list definition!

So, we have modified the program to remove the character definition of the [], see below:

tnames = ' python ', ' TCL ', ' Ruby '
CNAMEs = Stringvar ()
Cnames.set (Tnames)
L = Listbox (root, listvariable = Cnames,height = ten). Grid ()
As a result, the program displays correctly

How the hell did this thing get done? We'll change the code and print a few things.

Def changeitems ():
Print Cnames.get ()
tnames = ' python ', ' TCL ', ' Ruby ', ' Java '
Cnames.set (Tnames)

root = tkinter.tk ()
Root.geometry (' +400+200 ')
Root.minsize (400,200)
Root.title ("Test")

Tnames = [' Python ', ' TCL ', ' Ruby ']
Print tnames.__class__
CNAMEs = Stringvar ()
Cnames.set (Tnames)
L = Listbox (root, listvariable = Cnames,height = ten). Grid ()

Ttk. button (Root,text = "submit", command = changeitems). Grid ()

Root.mainloop ()

Execute the above code and output the following from the console:


<type ' list ' >
("[' Python ',", "' TCL ',", "' Ruby ']")

What does this mean? When the Stringvar type calls the Set function, the variable is first converted to the tuples type of data. So, it's actually better to use the tuples type of data directly before calling the set function, which might be a better point.

So how do you make changes to the function variables? It's really simple, define a list, and then convert the list to tuples. The modified function is as follows:

Def changeitems ():
Tnames.append (' Java ')
Cnames.set (Tuple (tnames))

root = tkinter.tk ()
Root.geometry (' +400+200 ')
Root.minsize (400,200)
Root.title ("Test")

Tnames = [' Python ', ' TCL ', ' Ruby ']
CNAMEs = Stringvar ()
Cnames.set (Tuple (tnames))
L = Listbox (root, listvariable = Cnames,height = ten). Grid ()

Ttk. button (Root,text = "submit", command = changeitems). Grid ()

Root.mainloop ()

4. Other functions of Stringvar:

Stringvar other functions Besides set include: Get () is used to return the value of the Stringvar variable, the trace (mode, callback) is used to invoke the callback function when some mode is triggered, and there are other functions. But what I'm using right now doesn't include these, so I'm just doing a record. For details, see the link below:

Python Basics-gui Programming-tk-stringvar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.