【问题标题】:I am developing a calculator in tkinter in python我正在用python的tkinter开发一个计算器
【发布时间】:2021-03-13 05:18:32
【问题描述】:

面临的问题是,当我单击按钮时,数字确实出现在条目中,但是当我单击下一个按钮时,前一个按钮被删除并且条目被更新为新数字。我想连接数字,以便输入所需的整数。

import tkinter as tk

root = tk.Tk()
root.title("Calculator")
root.geometry('300x300')
    

def num1():
    ss = 1
    link.set( ss)

def num2():
    ss = 2
    link.set(ss)

link = tk.StringVar()
entry = tk.Entry(root, width = 50 , textvariable = link).place(x = 0 , y = 20)
btn1 = tk.Button(root , height = 3 , width = 6 , text = "1", command = num1 ).place(x= 0, y = 100)
btn2 = tk.Button(root, height = 3 , width = 6 , text = "2", command = num2 ).place(x = 60 , y = 100)
btn3 = tk.Button(root, height = 3 , width = 6 , text = "3", ).place(x = 120 , y = 100)
btn4 = tk.Button(root, height = 3 , width = 6 , text = "4", ).place(x = 180 , y = 100)
btn5 = tk.Button(root, height = 3 , width = 6 , text = "5", ).place(x = 0 , y = 160)
btn6 = tk.Button(root, height = 3 , width = 6 , text = "6", ).place(x = 60 , y = 160)
btn7 = tk.Button(root, height = 3 , width = 6 , text = "7", ).place(x = 120 , y = 160)
btn8 = tk.Button(root, height = 3 , width = 6 , text = "8", ).place(x = 180 , y = 160)
btn9 = tk.Button(root, height = 3 , width = 6 , text = "9", ).place(x = 0 , y = 220)
btn0 = tk.Button(root, height = 3 , width = 6 , text = "0", ).place(x = 60 , y = 220)
btnplus = tk.Button(root, height = 3 , width = 6, text = "+", ).place(x = 240 , y = 100)
btnminus = tk.Button(root, height = 3 , width = 6 , text = "-", ).place(x = 240 , y = 160)
btnmultiply = tk.Button(root , height = 3 , width = 6 , text = "*").place(x = 240 , y = 220)
btnequals = tk.Button(root, height = 3 , width = 6 , text = "=", ).place(x = 120 , y = 220)
btndivide = tk.Button(root, height = 3, width = 6, text = "/").place(x = 180 , y = 220)

    
root.mainloop()

注意:我刚刚编写了用整数 1 和 2 命令按钮的函数

【问题讨论】:

  • 请通过intro tourhelp centerhow to ask a good question 了解本网站的运作方式并帮助您改进当前和未来的问题,从而帮助您获得更好的答案。 “告诉我如何解决这个编码问题?”与 Stack Overflow 无关。您必须诚实地尝试解决方案,然后就您的实施提出具体问题。 Stack Overflow 无意取代现有的教程和文档。
  • 几乎您发布的所有代码都与您的问题无关。您需要知道如何让按钮返回某种值;这是关于 tkinter 按钮的任何教程。您需要知道如何获取12 的值,并从中生成12;这要么是简单的位置算术,要么是字符串连接并转换为整数;这些都包含在有关其各自主题的任何教程中。你被困在哪里了?发布问题代码,而不是您的 GUI。

标签: python tkinter tkinter-entry


【解决方案1】:

你应该使用这样的东西:

# globally declare the screen output variable 
scr_output = "" 
 
 
# Function to update output 
def press(num): 
    # point out the global expression variable 
    global scr_output 
 
    # concatenation of string 
    scr_output = scr_output + str(num) 
 
    # update the output by using set method 
    equation.set(scr_output) 

【讨论】:

    猜你喜欢
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 2016-12-09
    • 2015-09-25
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    相关资源
    最近更新 更多