2014-12-30 15 views
0

我正在使用的程序目前需要用戶輸入以顯示在程序窗口中。我研究了互聯網和計算器,發現了幾個解決方案來解決我的問題,但似乎沒有任何工作。我的目標是通過Python的tkinter入口小部件接收來自用戶的輸入,並將結果顯示在一個新的標籤中,同時取出我的第一個和輸入框,但是,該程序拒絕我的答案。從Python的tkinter入口小部件接收輸入並將其顯示在標籤中

你有什麼策略,代碼/圖書館的條款,或者你有什麼建議來實現我的目標?

我現有的解決方案:

.get() 
textvariable=self.entdat 

現有的代碼如下:

from Tkinter import * 
import time 

class Input(Frame): 

    def __init__(self, parent=None, **kw): 
    Frame.__init__(self, parent, background="white") 
    self.parent = parent 
    self.initUI() 
    self.entdat = StringVar 
    self.timestr = StringVar() 
    self.makeWidgets() 

def makeWidgets(self): 
    self.ol = Label(text="Objective:") 
    self.ol.pack(side=TOP) 
    self.ew = Entry() 
    self.ew.pack(side=TOP) 
    self.b = Button(text="OK", command=self.clicked) 
    self.b.pack(side=TOP) 

def clicked(self): 
    self.entdat = self.ew.get() 
    self.dat = Label(textvariable=self.ew.get()) 
    self.dat.pack(side=TOP) 
    self.hide_Widget() 


def hide_Widget(event): 
    event.ew.pack_forget() 
    event.ol.pack_forget() 
    event.b.pack_forget() 

def main(): 
root = Tk() 
root.geometry("240x135+25+50") 
tm = Input(root) 
tm.pack(side=TOP) 

root.mainloop() 

if __name__ == '__main__': 
    main() 
+1

什麼是'self.initUI()'?它沒有在你提供的代碼中定義。 – Marcin

+0

對不起。我只包含了包含該問題的程序部分。 self.intitUI()將parent.title設置爲「Input」。 –

回答

0

我修改您的代碼,以便它至少執行,並希望在你想要的方式。

from Tkinter import * 

class Input(Frame): 
    def __init__(self, parent=None, **kw): 
     Frame.__init__(self, parent, background="white") 
     self.parent = parent 
     self.entdat = StringVar() 
     self.makeWidgets() 

    def makeWidgets(self): 
     self.ol = Label(text="Objective:") 
     self.ol.pack(side=TOP) 
     self.ew = Entry(textvariable=self.entdat) 
     self.ew.pack(side=TOP) 
     self.b = Button(text="OK", command=self.clicked) 
     self.b.pack(side=TOP) 

    def clicked(self): 
     self.dat = Label(self, textvariable=self.entdat) 
     self.dat.pack(side=TOP) 
     self.distroy_Widget() 


    def distroy_Widget(self): 
     self.ew.destroy() 
     self.ol.destroy() 
     self.b.destroy() 

def main(): 
    root = Tk() 
    root.geometry("240x135+25+50") 
    tm = Input(root) 
    tm.pack(side=TOP) 

    root.mainloop() 

if __name__ == '__main__': 
    main() 

希望它有幫助。

+0

非常感謝你,這正是我一直在尋找的。這對我來說意味着很多,你會花時間做到這一點。現在,它回到了我的編程! :-) –

+0

@Rick_Roll不用擔心。很高興我能幫上忙。 – Marcin

相關問題