简体   繁体   English

如何在 pygubu 中获取 tkinter 条目小部件值?

[英]how can I getting tkinter entry widget value in pygubu?

I use tkinter in pygubu.我在 pygubu 中使用 tkinter。 I want I get value of Entry_1 widget.我希望获得 Entry_1 小部件的价值。 the Entry_1 widget value is blue. Entry_1 小部件值为蓝色。 and textvariable of Entry_1 is 'text_1' I read pygubu document. Entry_1 的 textvariable 是 'text_1' 我阅读了 pygubu 文档。 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和我链接 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 (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回溯(最近一次调用):文件“C:\\Python34\\pygubu.py”,第 25 行,在 print(app.mainwindow.getvar('text_1')) 文件“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.使用 pygubu-designer 打开您的ask.ui文件,然后展开 Frame_2 并单击 Entry_1。 In the section below, under the General tab, you will see the textvariable is empty.在下面的部分中,在 General 选项卡下,您将看到textvariable为空。 In this field enter: entry1_var .在此字段中输入: entry1_var

For Entry_2 enter in the textvariable field: entry2_var and for Entry_3 enter in the textvariable field: entry3_var .对于 Entry_2,在textvariable字段中输入: entry2_var ,对于 Entry_3 在textvariable字段中输入: 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 .要单击 OK 按钮将蓝色、黄色和绿色这 3 个变量值打印到控制台,然后: 选择 Button_1 并在其命令字段中输入: button1_callback

On the main menubar, click File and Save the ask.ui file.在主菜单栏上,单击文件并保存ask.ui文件。

If you now look around through the contents of the ask.ui file the following four lines have been added to it...如果您现在查看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>

The following method is now added to the ask.py file:现在将以下方法添加到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"))

Also delete or comment out the line #print(app.mainwindow.getvar('text_1'))同时删除或注释掉#print(app.mainwindow.getvar('text_1'))

Your ask.py file should now look like this...你的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()

Run your python program and click on the OK button.运行你的python程序并点击OK按钮。 The console will display:控制台将显示:

$ python3 ask.py
blue
yellow
green

The third entry widget will change from displaying green to red .第三个条目小部件将从显示绿色变为红色

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 tkinter 中重复获取新行的值? 下面的代码仅获取 tkinter 条目小部件中的最后一行 - How can I get new row's value repeatedly in tkinter? The code below gets only the last row in tkinter entry widget 如何制作Tkinter条目小部件? - How do I make a tkinter entry widget? 如何从 python tkinter 条目中获取整数值? - how can i get a integar value from python tkinter entry? 如何从 tkinter python 中的特定条目之外通过鼠标左键单击来销毁小部件 - How can I destroy widget with mouse left click from outside of the specific entry in tkinter python 如何获得Tkinter条目小部件,使其与普通的python输入相同 - How can I get a Tkinter entry widget to work the same as a normal python input 如何在不使用 Tkinter 中的条目小部件的情况下制作一个按钮,当按下它时将产品名称显示到文本小部件 - how can i make a button that when pressed it shows the name of the product to a Text widget without using an Entry widget in Tkinter Python tkinter Entry小部件无值 - None Value with Python tkinter Entry widget 如何在 tkinter 中打印 Entry() 的值? - How can I print the values of a Entry() in tkinter? 如果 Tkinter 小部件中的条目已更正,如何使 tkinter 消息消失? - How do I make a tkinter message disappear if the entry in Tkinter Widget has been corrected? 如何在Tkinter Entry小部件中将文本与TOP对齐 - How to align text to TOP in a tkinter Entry widget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM