AttributeError:'function'对象没有属性'upper'

时间:2014-11-09 17:15:12

标签: python python-3.x tkinter

当我运行给定代码时,我收到错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\Users\Matthew\Desktop\Code\Functionised\encoder idea 2 GUI ATTEMPT.py", line 10, in encode
    m = m.upper()
AttributeError: 'function' object has no attribute 'upper'

我知道这与行有关 - m = m.upper()

但我不知道如何解决这个问题

import sys
import os.path
from tkinter import *

def encode():
    array = []
    temp_array = []
    i = 0
    m = message.get
    m = m.upper()
    array.append(m)
    o = offset.get()
    array.append(o)
    length = len(array[0])
    while length > i:
        temp = array[0][i]
        if temp == " ":
            temp_array.append(temp)
            i = i + 1
        elif temp == ".":
            temp_array.append(temp)
            i = i + 1
        elif (ord(temp) + o) <= 90 and (ord(temp) + o) >= 65:
            #print("Easy option")
            temp = ord(temp)
            temp = temp + o
            temp = chr(temp)
            temp_array.append(temp)
            i = i + 1
        else:
            #print("Hard option")
            temp = ord(temp)
            temp = temp + o
            temp = (temp % 90) + 64
            temp = chr(temp)
            temp_array.append(temp)
            i = i + 1
    i = i - 1
    temp = temp_array[i]
    while i > 0:
        i = i - 1
        temp = temp_array[i] + temp 
    array.append(temp)
    word = (array[2])
    print(word)
    my_file = open("messages.txt", "a") #Open the file messages or if it does not exist create it
    for item in array:              #Get all items in array
        my_file.write(str(item))    #Write them to file
        my_file.write("\n")         #New line
    my_file.close()                 #Close the file


gui = Tk()

gui.title("Caesar Cypher Encoder")

Button(gui, text="Encode", command=encode).grid(row = 3, column = 0)
Label(gui, text = "Message").grid(row = 1, column =0)
Label(gui, text = "Offset").grid(row = 1, column =1)
message = Entry(gui)
message.grid(row=2, column=0)
offset = Scale(gui, from_=1, to=25, orient=HORIZONTAL)
offset.grid(row=2, column=1)

mainloop( )

在有人要求之前 - 是的,这是我的控制评估 - 现在已经完成 - 我正在使用代码来学习更高级的功能 - 例如tkinter

2 个答案:

答案 0 :(得分:5)

更改

m = message.get

m = message.get()

否则,您将功能分配给m,而不是返回值。

答案 1 :(得分:0)

import sys
import os.path
from tkinter import *

def encode():
    array = []
    temp_array = []
    i = 0
    m = message.get #Change it to "m = message.get()" (without quotes)
    m = m.upper()
    array.append(m)
    o = offset.get()
    array.append(o)
    length = len(array[0])
    while length > i:
        temp = array[0][i]
        if temp == " ":
            temp_array.append(temp)
            i = i + 1
        elif temp == ".":
            temp_array.append(temp)
            i = i + 1
        elif (ord(temp) + o) <= 90 and (ord(temp) + o) >= 65:
            #print("Easy option")
            temp = ord(temp)
            temp = temp + o
            temp = chr(temp)
            temp_array.append(temp)
            i = i + 1
        else:
            #print("Hard option")
            temp = ord(temp)
            temp = temp + o
            temp = (temp % 90) + 64
            temp = chr(temp)
            temp_array.append(temp)
            i = i + 1
    i = i - 1
    temp = temp_array[i]
    while i > 0:
        i = i - 1
        temp = temp_array[i] + temp 
    array.append(temp)
    word = (array[2])
    print(word)
    my_file = open("messages.txt", "a") #Open the file messages or if it does not exist create it
    for item in array:              #Get all items in array
        my_file.write(str(item))    #Write them to file
        my_file.write("\n")         #New line
    my_file.close()                 #Close the file


gui = Tk()

gui.title("Caesar Cypher Encoder")

Button(gui, text="Encode", command=encode).grid(row = 3, column = 0)
Label(gui, text = "Message").grid(row = 1, column =0)
Label(gui, text = "Offset").grid(row = 1, column =1)
message = Entry(gui)
message.grid(row=2, column=0)
offset = Scale(gui, from_=1, to=25, orient=HORIZONTAL)
offset.grid(row=2, column=1)

mainloop()