2016-07-29 106 views
0

我需要幫助。我正在開發與tkinter的界面。在某個時候,當我在窗口中插入一個按鈕時,它不能正常工作,實際上,這是行不通的。我給我這個錯誤,當我按下按鈕:Python 3(.5.2)Tkinter

File  "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1550, in __call__ 
return self.func(*args) 
    File "/Users/Edoardo/Desktop/Progetti/Programming Projects/Python Projects/Encoder 1.0 alpha.py", line 277, in <lambda> 
encode = Button(text="ENCODE", command=lambda: encode()) 
TypeError: 'Button' object is not callable 

這是代碼(部分缺失的原因,之前寫整個程序,我想確保它的工作原理):

from tkinter import * 
import tkinter.messagebox 

# encode = {char : code} 
utf8int = { 
    "!": "\\33", 
    "\"": "\\34", 
    "#": "\\35", 
    "$": "\\36", 
    "%": "\\37", 
    "&": "\\38", 
    "'": "\\39", 
    "(": "\\40", 
    ")": "\\41", 
    "*": "\\42", 
    "+": "\\43", 
    ",": "\\44", 
    "-": "\\45", 
    ".": "\\46", 
    "/": "\\47", 
    "0": "\\48", 
    "1": "\\49", 
    "2": "\\50", 
    "3": "\\51", 
    "4": "\\52", 
    "5": "\\53", 
    "6": "\\54", 
    "7": "\\55", 
    "8": "\\56", 
    "9": "\\57", 
    ":": "\\58", 
    ";": "\\59", 
    "<": "\\60", 
    "=": "\\61", 
    ">": "\\62", 
    "?": "\\63", 
    "@": "\\64", 
    "A": "\\65", 
    "B": "\\66", 
    "C": "\\67", 
    "D": "\\68", 
    "E": "\\69", 
    "F": "\\70", 
    "G": "\\71", 
    "H": "\\72", 
    "I": "\\73", 
    "J": "\\74", 
    "K": "\\75", 
    "L": "\\76", 
    "M": "\\77", 
    "N": "\\78", 
    "O": "\\79", 
    "P": "\\80", 
    "Q": "\\81", 
    "R": "\\82", 
    "S": "\\83", 
    "T": "\\84", 
    "U": "\\85", 
    "V": "\\86", 
    "W": "\\87", 
    "X": "\\88", 
    "Y": "\\89", 
    "Z": "\\90", 
    "[": "\\91", 
    "\\": "\\92", 
    "]": "\\93", 
    "^": "\\94", 
    "_": "\\95", 
    "`": "\\96", 
    "a": "\\97", 
    "b": "\\98", 
    "c": "\\99", 
    "d": "\\100", 
    "e": "\\101", 
    "f": "\\102", 
    "g": "\\103", 
    "h": "\\104", 
    "i": "\\105", 
    "j": "\\106", 
    "k": "\\107", 
    "l": "\\108", 
    "m": "\\109", 
    "n": "\\110", 
    "o": "\\111", 
    "p": "\\112", 
    "q": "\\113", 
    "r": "\\114", 
    "s": "\\115", 
    "t": "\\116", 
    "u": "\\117", 
    "v": "\\118", 
    "w": "\\119", 
    "x": "\\120", 
    "y": "\\121", 
    "z": "\\122", 
    "{": "\\123", 
    "|": "\\124", 
    "}": "\\125", 
    "~": "\\126", 
    "€": "\\128", 
    "‚": "\\130", 
    "ƒ": "\\131", 
    "„": "\\132", 
    "…": "\\133", 
    "†": "\\134", 
    "‡": "\\135", 
    "ˆ": "\\136", 
    "‰": "\\137", 
    "Š": "\\138", 
    "‹": "\\139", 
    "Œ": "\\140", 
    "Ž": "\\142", 
    "‘": "\\145", 
    "’": "\\146", 
    "「": "\\147", 
    "」": "\\148", 
    "•": "\\149", 
    "–": "\\150", 
    "—": "\\151", 
    "˜": "\\152", 
    "™": "\\153", 
    "š": "\\154", 
    "›": "\\155", 
    "œ": "\\156", 
    "ž": "\\158", 
    "Ÿ": "\\159", 
    "¡": "\\161", 
    "¢": "\\162", 
    "£": "\\163", 
    "¤": "\\164", 
    "¥": "\\165", 
    "¦": "\\166", 
    "§": "\\167", 
    "¨": "\\168", 
    "©": "\\169", 
    "ª": "\\170", 
    "«": "\\171", 
    "¬": "\\172", 
    "®": "\\174", 
    "¯": "\\175", 
    "°": "\\176", 
    "±": "\\177", 
    "²": "\\178", 
    "³": "\\179", 
    "´": "\\180", 
    "µ": "\\181", 
    "¶": "\\182", 
    "·": "\\183", 
    "¸": "\\184", 
    "¹": "\\185", 
    "º": "\\186", 
    "»": "\\187", 
    "¼": "\\188", 
    "½": "\\189", 
    "¾": "\\190", 
    "¿": "\\191", 
    "À": "\\192", 
    "Á": "\\193", 
    "Â": "\\194", 
    "Ã": "\\195", 
    "Ä": "\\196", 
    "Å": "\\197", 
    "Æ": "\\198", 
    "Ç": "\\199", 
    "È": "\\200", 
    "É": "\\201", 
    "Ê": "\\202", 
    "Ë": "\\203", 
    "Ì": "\\204", 
    "Í": "\\205", 
    "Î": "\\206", 
    "Ï": "\\207", 
    "Ð": "\\208", 
    "Ñ": "\\209", 
    "Ò": "\\210", 
    "Ó": "\\211", 
    "Ô": "\\212", 
    "Õ": "\\213", 
    "Ö": "\\214", 
    "×": "\\215", 
    "Ø": "\\216", 
    "Ù": "\\217", 
    "Ú": "\\218", 
    "Û": "\\219", 
    "Ü": "\\220", 
    "Ý": "\\221", 
    "Þ": "\\222", 
    "ß": "\\223", 
    "à": "\\224", 
    "á": "\\225", 
    "â": "\\226", 
    "ã": "\\227", 
    "ä": "\\228", 
    "å": "\\229", 
    "æ": "\\230", 
    "ç": "\\231", 
    "è": "\\232", 
    "é": "\\233", 
    "ê": "\\234", 
    "ë": "\\235", 
    "ì": "\\236", 
    "í": "\\237", 
    "î": "\\238", 
    "ï": "\\239", 
    "ð": "\\240", 
    "ñ": "\\241", 
    "ò": "\\242", 
    "ó": "\\243", 
    "ô": "\\244", 
    "õ": "\\245", 
    "ö": "\\246", 
    "÷": "\\247", 
    "ø": "\\248", 
    "ù": "\\249", 
    "ú": "\\250", 
    "û": "\\251", 
    "ü": "\\252", 
    "ý": "\\253", 
    "þ": "\\254", 
    "ÿ": "\\255", 
    "Ā": "\\256", 
    "ā": "\\257", 
    "Ă": "\\258", 
    "ă": "\\259", 
    "Ą": "\\260", 
    "ą": "\\261", 
    "Ć": "\\262", 
    "ć": "\\263", 
    "Ĉ": "\\264", 
    "ĉ": "\\265", 
    "Ċ": "\\266", 
    "ċ": "\\267", 
    "Č": "\\268", 
    "č": "\\269", 
    "Ď": "\\270", 
    "ď": "\\271", 
    "Đ": "\\272", 
    "đ": "\\273" 
} 

utf8hex = { 

} 

Ascii = { 

} 

binary = { 

} 

root = Tk() 
root.resizable(0, 0) 
root.title("Encoder 1.0 (Alpha)") 

v = IntVar() 

infoLabel = Label(text="This is an encoder, please choose the encode type and enter a message ") 
infoLabel.grid(column=1, columnspan=3, row=1) 

RB_UTF8_int = Radiobutton(text="UTF-8 (int)", variable=v, value="utf-8 int") 
RB_UTF8_int.grid(column=1, row=2) 

RB_UTF8_hex = Radiobutton(text="UTF-8 (hex)", variable=v, value="utf-8 hex") 
RB_UTF8_hex.grid(column=2, row=2) 

RB_Ascii = Radiobutton(text="Ascii", variable=v, value="Ascii") 
RB_Ascii.grid(column=1, row=3) 

RB_UTF16 = Radiobutton(text="Binary", variable=v, value="binary") 
RB_UTF16.grid(column=2, row=3) 

message = Entry() 
message.grid(column=1, columnspan=4, row=4) 

encode = Button(text="ENCODE", command=lambda: encode()) 
encode.grid(column=1, columnspan=4, row=5) 

root.mainloop() 


def encode(): 
    errors = 0 
    decoded_message = [] 
    encoded_message = [] 
    for i in message.get(): 
     decoded_message.append(i) 
    if message.get() != "": 
     if v == "utf-8 int": 
      for i in decoded_message: 
       for char, code in utf8int: 
        if char == i: 
         encoded_message.append(code) 
        else: 
         encoded_message.append("(error)") 
         errors += 1 
         pass 
      decoded_message1 = str(decoded_message) 
      decoded_message2 = decoded_message1.replace("[", "") 
      decoded_message3 = decoded_message2.replace("]", "") 
      decoded_message4 = decoded_message3.replace(", ", "") 
      decoded_message5 = decoded_message4.replace("'", "") 
      if tkinter.messagebox.askyesno(title="Succesfully Generated", message="Your code is succesfully " 
                        "generated\n" 
                        "with"+str(errors)+"errors.\n" 
                        "This is your code:\n" + 
                        decoded_message5 + 
                        "Copy it on the clipboard?"): 
       root.clipboard_clear() 
       root.clipboard_append(decoded_message5) 
      else: 
       pass 
     elif v == "utf-8 hex": 
      for i in decoded_message: 
       for char, code in utf8hex: 
        if char == i: 
         encoded_message.append(code) 
        else: 
         errors += 1 
         pass 
     elif v == "Ascii": 
      for i in decoded_message: 
       for char, code in Ascii: 
        if char == i: 
         encoded_message.append(code) 
        else: 
         errors += 1 
         pass 
     elif v == "binary": 
      for i in decoded_message: 
       for char, code in binary: 
        if char == i: 
         encoded_message.append(code) 
        else: 
         errors += 1 
         pass 
     else: 
      tkinter.messagebox.showerror(title="FATAL ERROR", message="Please, select an encode type.") 
    else: 
     tkinter.messagebox.showerror(title="ERROR 404", message="ERROR 404: message not found!") 

回答

1

你有一個定義問題:你的回調定義永遠不會達到,因爲之前調用了Tkinter的mainloop。當按下按鈕時會發生什麼,它會嘗試調用按鈕對象本身,而不是下面定義的函數encode

要讓它按原樣工作,只需將root.mainloop()移動到腳本的最後一行即可。

除此之外,您正在使用相同的名稱調用Button和您的回調函數。如果在定義回調函數後啓動Tkinter循環,並且之後不需要訪問Button本身,那麼這將起作用(並且可能在其他版本的程序中起作用)。你應該使用不同的名字。

但是,比不同的名稱更好,試圖將您的接口定義代碼放入程序根級別而不是函數內部是一個組織問題。

+0

如果你移動_only_'root.mainloop()',你會得到'NameError',因爲你試圖調用的函數沒有被定義。這就是爲什麼我說「你應該移動你的函數定義」,它解決了NameError和mainloop。 – Lafexlos

+0

它不會導致「naemerror」,因爲在該代碼中包含一個「lambda」表達式:它將僅在「點擊時間」進行評估。 – jsbueno

+1

哦拍。完全錯過了。你說得對,對不起。 :) – Lafexlos