简体   繁体   English

Tkinter中使用Python 3的计算器程序的问题

[英]issue with a calculator program in Tkinter with Python 3

here is the code (see below for edits): 这是代码(有关修改,请参见下文):

from tkinter import *

my_int_var = 0

def button_1_method():      #what's important is this method and the button_1 defined below
    my_int_var.set = my_int_var.get + 1
    entry_field.set = my_int_var

root = Tk()
frame_1 = Frame(root)
frame_1.pack()
frame_2 = Frame(root)
frame_2.pack()
frame_3 = Frame(root)
frame_3.pack()
frame_4=Frame(root)
frame_4.pack()

entry_field = Entry(frame_4)
entry_field.pack()

button_add = Button(frame_1, text = "add")
button_subtract = Button(frame_1, text = "subtract")
button_multiply = Button(frame_1, text = "multiply")
button_divide = Button(frame_1, text = "divide")
button_calculate = Button(frame_3, text = "calculate")
button_calculate.pack()

button_add.grid(row = 0)
button_subtract.grid(row = 0, column = 1)
button_multiply.grid(row=0,column= 2)
button_divide.grid(row=0,column = 3)

button_1 = Button(frame_2,text = "1", command = button_1_method())
button_2 = Button(frame_2,text = "2")
button_3 = Button(frame_2,text = "3")
button_4 = Button(frame_2,text = "4")
button_5 = Button(frame_2,text = "5")
button_6 = Button(frame_2,text = "6")
button_7 = Button(frame_2,text = "7")
button_8 = Button(frame_2,text = "8")
button_9 = Button(frame_2,text = "9")
button_0 = Button(frame_2,text = "0")

button_1.grid(row = 0)
button_2.grid(row = 0,column = 1)
button_4.grid(row=0, column= 3)
button_5.grid(row=0, column= 4)
button_6.grid(row=0, column= 5)
button_7.grid(row=0, column= 6)
button_8.grid(row=0, column= 7)
button_9.grid(row=0, column= 8)
button_0.grid(row=0, column= 9)

root.mainloop()

I am trying to add '1' to the number displayed in the entry whenever the 'one' button is pushed. 每当按“一个”按钮时,我都试图在条目中显示的数字上添加“ 1”。 however I am playing around with the button_1_method and getting various errors. 但是我在玩button_1_method并遇到各种错误。 How can I fix it so that whenever I press the one button, a '1' is added to the number in the entry (and the same with the rest of the number buttons)? 如何解决该问题,以便每当我按下一个按钮时,在条目中的数字上都会添加一个“ 1”(其余数字按钮也是如此)?

EDIT: I changed it a little: 编辑:我改变了一点:

my_str_var = StringVar()

def button_1_method(my_str_var):
    my_str_var.set(my_str_var.get() + 1)
    entry_field.set(my_str_var)


button_1 = Button(frame_2,text = "1", command = button_1_method)

now this is the error I'm getting: 现在这是我得到的错误:

Traceback (most recent call last):
  File "C:/Users/TOTTY/PycharmProjects/beginnerpoject/python calculator.py", line 5, in <module>
    my_str_var = StringVar()
  File "C:\Python34\lib\tkinter\__init__.py", line 332, in __init__
    Variable.__init__(self, master, value, name)
  File "C:\Python34\lib\tkinter\__init__.py", line 236, in __init__
    self._root = master._root()
AttributeError: 'NoneType' object has no attribute '_root'

set is a method , which you have to call . set是一个方法 ,你必须调用 And same for get . 和同样get

So, instead of this: 因此,代替此:

my_int_var.set = my_int_var.get + 1

Do this: 做这个:

my_int_var.set(my_int_var.get() + 1)

But you've got another problem on top of that. 但是除此之外,您还有另一个问题。 my_int_var isn't a Tk IntVar , it's just a plain old int, which has no such methods. my_int_var不是Tk IntVar ,它只是一个普通的int,没有此类方法。 And it's also not attached to your Entry widget in any way. 而且它也没有以任何方式附加到您的Entry小部件。

So, you need to create a Tk var with my_int_var = IntVar() , and then you need to pass it to the widget either in the constructor or in a config call. 因此,您需要使用my_int_var = IntVar()创建一个Tk var,然后需要在构造函数或config调用中将其传递给小部件。

If you look at the docs where you found the get and set methods, you'll see some examples that should make it obvious. 如果查看在其中找到get和set方法的文档,则会看到一些使它变得显而易见的示例。 In particular, see the third example under Patterns in the Entry docs (but make sure to read the whole section). 特别是,请参阅Entry文档中Patterns下的第三个示例(但请务必阅读整个部分)。


There are other problems beyond the ones directly related to your error. 除了与您的错误直接相关的问题以外,还有其他问题。 For example: 例如:

button_1 = Button(frame_2,text = "1", command = button_1_method())

You want to pass the function, button_1_method as the command. 您要传递函数button_1_method作为命令。 But that's not what you're doing; 但这不是你在做什么; you're calling that function, getting its return value, and passing that as the command. 您正在调用该函数,获取其返回值,并将作为命令传递。 (It's exactly the opposite of your first problem, where you want to call a function but left the parens off. You can't expect Python to guess when you want to call a function and when you want to use it as a value; the parentheses are how you tell it to call the function.) (这与您第一个问题的相反,在第一个问题中,您想调用一个函数,但不用担心。您不能期望Python会在何时调用函数以及何时将其用作值时进行猜测;括号就是您告诉它调用函数的方式。)

Similarly, as I mentioned in a comment, even if you get this all right, adding 1 to a number is not what the 1 key on a calculator does. 同样,正如我在评论中提到的那样,即使您没问题,将1加到数字上也不是计算器上的1键所能实现的。 You need to get the difference between a number and its string representation straight or you're going to have a lot more problems with this app. 您需要直接弄清数字与其字符串表示形式之间的差异,否则此应用程序将遇到更多问题。 1 != '1' , but str(1) == '1' and 1 == int('1') 1 != '1' ,但是str(1) == '1' and 1 == int('1')

In your updated version, you've changed it to use a StringVar , whose value is of course a string, and you're trying to add 1 to its value. 在更新的版本中,您已将其更改为使用StringVar ,其值当然是字符串,并且您尝试将其值加1 That's the same problem. 那是同样的问题。 Again, the number 1 and the string '1' are not the same thing. 同样,数字1和字符串'1'不是同一件事。

And you're also trying to call a set method on an Entry object when no such method exists. 而且,当不存在这样的方法时,您也尝试在Entry对象上调用set方法。 Read the Entry docs to see how to change or replace the value of an Entry —but if you're using a StringVar (as in your edited code), you don't need to do this in the first place. 阅读Entry文档以了解如何更改或替换Entry的值-但是,如果您使用StringVar (如在已编辑的代码中一样),则无需首先执行此操作。 That's the whole point of binding a variable to a widget; 这就是将变量绑定到小部件的全部要点。 changes to either one affect the other. 对任何一个的更改都会影响另一个。 So, when you call my_str_var.get , you're getting the current contents of the Entry widget, and when you call my_str_var.set , you're replacing the contents of the widget. 因此,当您调用my_str_var.get ,您将获得Entry小部件的当前内容,而当您调用my_str_var.set ,您将替换该小部件的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM