Python:基于 GUI 的“反向输入”使用递归失败

发布于 2025-01-11 14:07:53 字数 704 浏览 0 评论 0原文

我正在尝试编写一个Python程序,要求用户输入一个数字,然后使用递归反转它。我的尝试如下,但我的代码给了我 TypeError: unsupported operand type(s) for //: 'Entry' and 'int' - 有什么想法吗?

from tkinter import * 

def reverseInteger(n, r):
    if n==0:
        return r
    else:
        return reverseInteger(n//10, r*10 + n%10)

window = Tk() 
window.title("Reverse Integer")

frame1 = Frame(window) 
frame1.pack()

number = StringVar()
numEntry = Entry(frame1, textvariable=number) 
btGetName = Button(frame1, text = "Calculate", command = reverseInteger(numEntry, 0)) 
label3 = Label(frame1)

numEntry.grid(row = 1, column = 1)
btGetName.grid(row = 1, column = 2)
label3.grid(row = 2, column = 1, sticky="w")

window.mainloop() 

I am trying to make a Python program that asks the user for a number then reverses it using recursion. My attempt is below, but my code gives me TypeError: unsupported operand type(s) for //: 'Entry' and 'int' - any ideas?

from tkinter import * 

def reverseInteger(n, r):
    if n==0:
        return r
    else:
        return reverseInteger(n//10, r*10 + n%10)

window = Tk() 
window.title("Reverse Integer")

frame1 = Frame(window) 
frame1.pack()

number = StringVar()
numEntry = Entry(frame1, textvariable=number) 
btGetName = Button(frame1, text = "Calculate", command = reverseInteger(numEntry, 0)) 
label3 = Label(frame1)

numEntry.grid(row = 1, column = 1)
btGetName.grid(row = 1, column = 2)
label3.grid(row = 2, column = 1, sticky="w")

window.mainloop() 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三人与歌 2025-01-18 14:07:53

你的递归函数非常好,但还有其他几个
您的代码中存在问题。

主要的是Buttoncommand参数必须是
当用户按下按钮时将调用的函数。在
您的代码中,command 设置为 reverseInteger 的返回值
这是一个整数。所以这里有一个问题。

另外,在我看来,你想把你的结果
label3 中的计算,因此您的 StringVar 应附加到它
而不是 numEntry

所以这是一个对我来说似乎不错的版本:

from tkinter import *

def reverseInteger(n, r):
    if n==0:
        return r
    else:
        return reverseInteger(n//10, r*10 + n%10)

def reverse():  # called when the user click on the button
    value = reverseInteger(int(numEntry.get()), 0)
    number.set(value)  # change the text of the label

window = Tk() 
window.title("Reverse Integer")

frame1 = Frame(window) 
frame1.pack()

number = StringVar()
numEntry = Entry(frame1) 
btGetName = Button(frame1, text = "Calculate", command = reverse) 
label3 = Label(frame1, textvariable=number)

numEntry.grid(row = 1, column = 1)
btGetName.grid(row = 1, column = 2)
label3.grid(row = 2, column = 1, sticky="w")

window.mainloop()

Your recursive function is perfectly fine but there are several other
problems in your code.

The main one is that the command parameter of Button must be the
function that will be called when the user presses on the buttton. In
your code, command is set to the return value of reverseInteger
which is an int. So there is a problem here.

Also it seems to me that you want to put the result of your
calculation in label3 so your StringVar should be attached to it
and not to numEntry.

So here is a version that seems ok to me:

from tkinter import *

def reverseInteger(n, r):
    if n==0:
        return r
    else:
        return reverseInteger(n//10, r*10 + n%10)

def reverse():  # called when the user click on the button
    value = reverseInteger(int(numEntry.get()), 0)
    number.set(value)  # change the text of the label

window = Tk() 
window.title("Reverse Integer")

frame1 = Frame(window) 
frame1.pack()

number = StringVar()
numEntry = Entry(frame1) 
btGetName = Button(frame1, text = "Calculate", command = reverse) 
label3 = Label(frame1, textvariable=number)

numEntry.grid(row = 1, column = 1)
btGetName.grid(row = 1, column = 2)
label3.grid(row = 2, column = 1, sticky="w")

window.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文