2016-05-29 110 views
1

我在pygubu中使用tkinter。我想我得到Entry_1小部件的價值。 Entry_1小部件值爲藍色。和Entry_1的textvariable是'text_1' 我讀pygubu文件。但我不明白。請問誰能知道我容易。如何在pygubu中獲取tkinter入口小部件的值?

和我鏈接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

回答

1

使用pygubu設計師打開你的ask.ui文件,然後展開Frame_2並點擊Entry_1。在下面的部分中,在常規選項卡下,您將看到文本變量爲空。在此字段中輸入: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> 

下面的方法現在被添加到問。 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( '文本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 

第三個條目小部件將顯示綠色紅色改變。

相關問題