[英]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.