我正在使用的程序允许用户输入文本并检查它是否是回文。输入使用order()进行排序,使用testPalindrome(orderedText)进行测试,然后用showResult(Palindrome)显示结果。当保存回文时,我的问题出现了。如果已确认输入是回文输入,则用户应该能够保存该输入。我认为如果在测试输入为真之前禁用保存按钮,这将是有意义的。所以我在showResult(Palindrome)中放入了“ saveButton.config(state ='normal')”,但这是行不通的。错误显示“未定义全局名称'saveButton'”,我明白了为什么。但是,我无法做到正确。我想知道是否可以通过在此单行文本中添加一些内容来解决此问题,或者我是否需要进行更彻底的修改。 (例如,我尝试将“ saveButton”变成一个全局变量,例如“ inputEntry”,但由于某种原因,我也无法使它起作用。)
import Tkinter
root = Tkinter.Tk()
infoLabel = Tkinter.Label(root, text = 'Enter a text and check if it is a palindrome.', height = 12, width = 64, bg = 'green')
inputEntry = Tkinter.Entry(root, width = 64)
def main():
window()
Tkinter.mainloop()
def window():
root.title('Palindrome detector')
root.geometry('534x285+500+250')
infoLabel.grid(row = 0, rowspan = 1, column = 0, columnspan = 4, pady = 8, padx = 8)
inputEntry.insert(0, 'Enter text...')
inputEntry.grid(row = 1, rowspan = 1, column = 0, columnspan = 4, padx = 4)
verifyButton = Tkinter.Button(root, text = 'Verify', width = 7, command = order).grid(row = 2, column = 0, pady = 10)
saveButton = Tkinter.Button(root, text = 'Save', state = 'disabled', width = 7, command = save).grid(row = 2, column = 3)
def order():
Text_input = inputEntry.get()
orderedText = Text_input.replace(" ", "")
orderedText = orderedText.lower()
orderedText = filter(str.isalnum, orderedText)
if any(char.isalpha() or char.isdigit() for char in orderedText):
testPalindrome(orderedText)
else:
infoLabel.config(text = 'The input must contain at least one alphanumeric character.', bg = 'red')
def testPalindrome(orderedText):
Palindrome = True
i = 0
for char in orderedText:
if orderedText[i] == orderedText[-(i+1)]:
Palindrome = True
i += 1
else:
Palindrome = False
break
showResult(Palindrome)
def showResult(Palindrome):
if Palindrome == True:
infoLabel.config(text = 'Yes! "' + str(inputEntry.get()) + '" is a palindrome!', bg = 'green')
saveButton.config(state = 'normal')
else:
infoLabel.config(text = 'No! "' + str(inputEntry.get()) + '" is not a palindrome!', bg = 'red')
saveButton.config(state = 'disabled')
def save():
savePalindrome = inputEntry.get()
palindromes_file = open("Palindromes.txt", "a")
palindromes_file.write(savePalindrome + "\n")
palindromes_file.close()
infoLabel.config(text = '"' + str(savePalindrome) + '" has been saved to file.', bg = 'green')
saveButton.config(state = 'disabled')
if __name__ == '__main__':
main()
编辑:阅读答案后,我遵循了一本教科书,尝试使用类重写整个过程。结果肯定会有很多缺陷。我现在关心的是“ NameError:未定义全局名称'testPalindrome'”。错误关于命令第7行(自己)。我看不到为什么这条线以及order(self)和testPalindrome(self)之间的“桥梁”突然成为问题?
import Tkinter
import tkMessageBox
root = Tkinter.Tk()
class MyGUI:
def __init__(self, master):
self.master = master
master.title('Palindrome detector')
master.geometry('534x285+500+250')
self.infoLabel = Tkinter.Label(self.master, text = 'Enter a text and check if it is a palindrome.', height = 12, width = 64, bg = 'green')
self.inputEntry = Tkinter.Entry(self.master, width = 64)
self.infoLabel.grid(row = 0, rowspan = 1, column = 0, columnspan = 4, pady = 8, padx = 8)
self.inputEntry.insert(0, 'Enter text...')
self.inputEntry.grid(row = 1, rowspan = 1, column = 0, columnspan = 4, padx = 4)
self.verifyButton = Tkinter.Button(self.master, text = 'Verify', width = 7, command = self.order)
self.verifyButton.grid(row = 2, column = 0, pady = 10)
self.saveButton = Tkinter.Button(self.master, text = 'Save', state = 'disabled', width = 7, command = self.save)
self.saveButton.grid(row = 2, column = 3)
def order(self):
self.Text_input = self.inputEntry.get()
self.orderedText = self.Text_input.replace(" ", "")
self.orderedText = self.orderedText.lower()
self.orderedText = filter(str.isalnum, self.orderedText)
if any(char.isalpha() or char.isdigit() for char in self.orderedText):
testPalindrome(self)
else:
self.infoLabel.config(text = 'The input must contain at least one alphanumeric character.', bg = 'red')
def testPalindrome(self):
self.Palindrome = True
i = 0
for char in self.orderedText:
if self.orderedText[i] == self.orderedText[-(i+1)]:
self.Palindrome = True
i += 1
else:
self.Palindrome = False
break
showResult(self)
def showResult(self):
if self.Palindrome == True:
self.infoLabel.config(text = 'Yes! "' + str(self.inputEntry.get()) + '" is a palindrome!', bg = 'green')
self.saveButton.config(state = 'normal')
else:
self.infoLabel.config(text = 'No! "' + str(self.inputEntry.get()) + '" is not a palindrome!', bg = 'red')
self.saveButton.config(state = 'disabled')
def save(self):
self.savePalindrome = self.inputEntry.get()
self.palindromes_file = open("Palindromes.txt", "a")
self.palindromes_file.write(self.savePalindrome + "\n")
self.palindromes_file.close()
self.infoLabel.config(text = '"' + str(self.savePalindrome) + '" has been saved to file.', bg = 'green')
self.saveButton.config(state = 'disabled')
my_gui = MyGUI(root)
Tkinter.mainloop()
最佳答案
您在函数中创建saveButton
,因此它是局部变量,在其他函数中不可用。您可以在global saveButton
中使用window()
将此变量设置为全局变量。
def window():
global saveButton
# ....
saveButton = Tkinter.Button(...)
saveButton.grid(...)
-
正如Kevin所说:
这条线
saveButton = Tkinter.Button(...).grid(...)
将
None
分配给saveButton
,因为grid()
总是返回None
采用
saveButton = Tkinter.Button(...)
saveButton.grid(...)
要么
Tkinter.Button(...).grid(...)
如果不需要
saveButton
变量。关于python - 在Tkinter中更改按钮状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34599658/