2017-01-11 31 views
0

我是新來的Python,剛開始學習class和tkinter,所以請原諒我「混亂」的代碼。 我試着輸入一些字符串到現場NR1,並點擊一個按鈕,打印後此字符串控制檯和存儲這個值後:python pass可變tkinter

from tkinter import Tk, BOTH, RIGHT, RAISED, BOTTOM, TOP, X, StringVar 
from tkinter.ttk import Frame, Button, Entry 


class AD(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self, parent, v=None, raw_input=None) 
     self.parent = parent 
     self.parent.geometry("250x150+300+300") 
     self.parent.title("Trolollo") 
     self.parent.resizable(False, False) 
     self.inp = None 
     self.v = StringVar() 
     self.raw_input = None 

     self.initUI() 

    def user_input(self): 
     global inp 
     a = self.raw_input(self.v.get()) 
     inp = a 
     return inp 


    def initUI(self): 
     self.pack(fill=BOTH, expand=True) 

     frame = Frame(self, relief=RAISED, borderwidth=0) 
     frame.pack(fill=BOTH, expand=True) 

     self.entry1 = Entry(frame, textvariable=self.v) 
     self.entry1.pack(side=TOP, fill=X, expand=False, padx=2, pady=2) 
     self.entry1.focus_set() 

     rename_button = Button(frame, text="Dispaly text", command =   self.user_input()) 
     rename_button.pack(side=TOP, expand=False, padx=2, pady=2) 

     entry2 = Entry(frame) 
     entry2.pack(side=TOP, fill=X, expand=False, padx=2, pady=2) 


     quit_button = Button(self, text="Quit", command=self.quit) 
     quit_button.pack(side=RIGHT, padx=5, pady=5) 

     ok_button = Button(self, text="OK") 
     ok_button.pack(side=RIGHT, padx=5, pady=5) 


def main(): 
    root = Tk() 


    app = AD(root) 
    root.mainloop() 


if __name__ == '__main__': 
    main() 

執行代碼後,我得到: 類型錯誤:「NoneType '對象不是可調用

任何幫助我理解

+2

簡而言之,'command = self.user_input()'移除缺口。 – Lafexlos

+1

'raw_input'是'None',並且您正嘗試在'a = self.'行中使用parantheses來調用它。不知道你想在那裏達到什麼。 – Lafexlos

+0

你使用的是Python 2還是3?你調用'raw_input()'這是python 2,但你導入'tkinter',而不是'Tkinter',這意味着python 3.然後你標記python-3.5 – tburrows13

回答

1

問題:

  1. rename_button的選項「command = self.user_input()」中放置的第一個問題。假設您命名爲功能 並且不執行該功能。放入()符號意味着您在加載代碼時執行了該功能,即,在沒有按重命名按鈕時執行一次 。
  2. 第二個問題是您的功能中的錯誤代碼user_input。這導致你的錯誤消息。

答案:帶有建議更正的代碼。

from tkinter import * 
from tkinter.ttk import * 


class AD(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self, parent, v=None, raw_input=None) 
     self.parent = parent 
     self.parent.geometry("250x150+300+300") 
     self.parent.title("Trolollo") 
     self.parent.resizable(False, False) 
     self.inp = None 
     self.v = StringVar() 
     self.raw_input = None 

     self.initUI() 

    def user_input(self): 
     # Get entry1 value, store it as an attribute and print to console 
     self.raw_input = self.v.get() 
     print(self.raw_input) 


    def initUI(self): 
     self.frame = Frame(self, relief=RAISED, borderwidth=0) 
     self.frame.pack(fill=BOTH, expand=True) 

     self.entry1 = Entry(self.frame, textvariable=self.v) 
     self.entry1.pack(side=TOP, fill=X, expand=False, padx=2, pady=2) 
     self.entry1.focus_set() 


     #self.rename_button = Button(self.frame, text="Dispaly text", 
     #       command = self.user_input()) 
     self.rename_button = Button(self.frame, text="Display text", 
            command = self.user_input) 
     self.rename_button.pack(side=TOP, expand=False, padx=2, pady=2) 


     # You can remove the triple quotes to display these widgets 
     """ 
     self.entry2 = Entry(self.frame) 
     self.entry2.pack(side=TOP, fill=X, expand=False, padx=2, pady=2) 


     self.quit_button = Button(self.frame, text="Quit", command=self.quit) 
     self.quit_button.pack(side=RIGHT, padx=5, pady=5) 

     self.ok_button = Button(self.frame, text="OK") 
     self.ok_button.pack(side=RIGHT, padx=5, pady=5) 

     """ 

     self.pack(fill=BOTH, expand=True) 


def main(): 
    root = Tk() 


    app = AD(root) 
    root.mainloop() 

你的GUI:enter image description here

幾點建議:

  • 千萬記得把自我。在你的小部件前面。
  • 一次測試一個小部件以幫助您調試代碼。
+0

這正是我想要做的 - 非常感謝。我不確定我是否正確理解每一個(this()函數名稱)。 – Fangir

+1

@Fangir歡迎您。當你在IDLE命令行輸入一個函數的名字時,python會創建一個函數對象。然而,當你在name和()之間輸入函數名後加()時,你會看到python運行/執行你的函數(就像獲取函數對象來執行它的工作)。我的2美分價值... –