简体   繁体   中英

how can I getting tkinter entry widget value in pygubu?

I use tkinter in pygubu. I want I get value of Entry_1 widget. the Entry_1 widget value is blue. and textvariable of Entry_1 is 'text_1' I read pygubu document. but I don't understand. who can know me easy please.

and I Link ask.ui file to use pygubu http://www.joinsland114.mireene.com/data/ask.ui

try:
    import tkinter as tk  # for python 3
except:
    import Tkinter as tk  # for python 2
import pygubu
from tkinter import *

class Application:
    def __init__(self, master):

        #1: Create a builder
        self.builder = builder = pygubu.Builder()

        #2: Load an ui file
        builder.add_from_file('ask.ui')

        #3: Create the widget using a master as parent
        self.mainwindow = builder.get_object('Frame_1', master)

        builder.connect_callbacks(self)


root = tk.Tk()
app = Application(root)

print(app.mainwindow.getvar('text_1'))    
root.mainloop()

Traceback (most recent call last): File "C:\\Python34\\pygubu.py", line 25, in print(app.mainwindow.getvar('text_1')) File "C:\\Python34\\lib\\tkinter__init__.py", line 454, in getvar return self.tk.getvar(name) _tkinter.TclError: can't read "text_1": no such variable

Use pygubu-designer to open your ask.ui file, then expand Frame_2 and click on Entry_1. In the section below, under the General tab, you will see the textvariable is empty. In this field enter: entry1_var .

For Entry_2 enter in the textvariable field: entry2_var and for Entry_3 enter in the textvariable field: entry3_var .

To make clicking on the OK button print to the console the 3 variable values of blue, yellow and green, then: Select Button_1 and in its command field enter: button1_callback .

On the main menubar, click File and Save the ask.ui file.

If you now look around through the contents of the ask.ui file the following four lines have been added to it...

<property name="textvariable">string:entry1_var</property>
<property name="textvariable">string:entry2_var</property>
<property name="textvariable">string:entry3_var</property>
<property name="command">button1_callback</property>

The following method is now added to the ask.py file:

def button1_callback(self):
    "Display the values of the 3 x Entry widget variables"
    print(self.builder.tkvariables['entry1_var'].get())
    print(self.builder.tkvariables['entry2_var'].get())
    print(self.builder.tkvariables['entry3_var'].get())

    # Change Entry_3 from green to red 
    self.builder.tkvariables['entry3_var'].set("red"))

Also delete or comment out the line #print(app.mainwindow.getvar('text_1'))

Your ask.py file should now look like this...

try:
    import tkinter as tk  # for python 3
except:
    import Tkinter as tk  # for python 2
import pygubu
from tkinter import *

class Application:
    def __init__(self, master):

        #1: Create a builder
        self.builder = builder = pygubu.Builder()

        #2: Load an ui file
        builder.add_from_file('ask.ui')

        #3: Create the widget using a master as parent
        self.mainwindow = builder.get_object('Frame_1', master)

        builder.connect_callbacks(self)

    def button1_callback(self):
        "Display the values of the 3 x Entry widget variables"
        print(self.builder.tkvariables['entry1_var'].get())
        print(self.builder.tkvariables['entry2_var'].get())
        print(self.builder.tkvariables['entry3_var'].get())

        # Change Entry_3 from green to red 
        self.builder.tkvariables['entry3_var'].set("red")

root = tk.Tk()
app = Application(root)

#print(app.mainwindow.getvar('text_1')) <-- This is commented out   
root.mainloop()

Run your python program and click on the OK button. The console will display:

$ python3 ask.py
blue
yellow
green

The third entry widget will change from displaying green to red .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM