如何在pygubu中获取tkinter入口小部件值?

时间:2016-05-29 05:41:07

标签: python-3.x tkinter

我在pygubu中使用tkinter。我想要获得Entry_1小部件的价值。 Entry_1小部件值为蓝色。 Entry_1的textvariable为'text_1' 我读了pygubu文件。但我不明白。 谁能够轻松地认识我。

和我链接ask.ui文件以使用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(最近一次调用最后一次):文件“C:\ Python34 \ pygubu.py”,行   25,在       print(app.mainwindow.getvar('text_1'))文件“C:\ Python34 \ lib \ tkinter__init __。py”,第454行,在getvar中       return self.tk.getvar(name)   _tkinter.TclError:无法读取“text_1”:没有这样的变量

1 个答案:

答案 0 :(得分:1)

使用pygubu-designer打开 ask.ui 文件,然后展开Frame_2并单击Entry_1。在下面的“常规”选项卡下,您将看到 textvariable 为空。在此字段中输入: entry1_var

对于Entry_2,请输入 textvariable 字段: entry2_var ,对于Entry_3,请输入 textvariable 字段: entry3_var

要点击OK按钮,在控制台上输入蓝色,黄色和绿色的3个变量值,然后:选择Button_1并在其命令字段中输入: button1_callback

在主菜单栏上,点击文件并保存 ask.ui 文件。

如果您现在浏览 ask.ui 文件的内容,则会添加以下四行...

<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>

以下方法现已添加到 ask.py 文件中:

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"))

同时删除或注释掉行 #print(app.mainwindow.getvar('text_1'))

您的 ask.py 文件现在应该如下所示......

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()

运行python程序并单击“确定”按钮。控制台将显示:

$ python3 ask.py
blue
yellow
green

第三个条目小部件将从显示绿色更改为红色