![](/img/trans.png)
[英]How can I get new row's value repeatedly in tkinter? The code below gets only the last row in tkinter entry widget
[英]how can I getting tkinter entry widget value in pygubu?
我在 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()
回溯(最近一次調用):文件“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
使用 pygubu-designer 打開您的ask.ui文件,然后展開 Frame_2 並單擊 Entry_1。 在下面的部分中,在 General 選項卡下,您將看到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程序並點擊OK按鈕。 控制台將顯示:
$ python3 ask.py
blue
yellow
green
第三個條目小部件將從顯示綠色變為紅色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.