Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更改代码,以便在按下按钮时,它显示在输入框中?_Python_Tkinter - Fatal编程技术网

Python 如何更改代码,以便在按下按钮时,它显示在输入框中?

Python 如何更改代码,以便在按下按钮时,它显示在输入框中?,python,tkinter,Python,Tkinter,我正在使用Python3上的tkinter制作一个计算器。我已经完成了布局。现在我正在尝试获取它,这样当我按下一个按钮时,它就会显示在输入框中。例如,如果我按2按钮,然后按+按钮,然后按3按钮,我希望它显示在程序顶部的输入框中 import tkinter import tkinter.messagebox class myGUI: def __init__(self): self.main_window = tkinter.Tk() self.mai

我正在使用Python3上的tkinter制作一个计算器。我已经完成了布局。现在我正在尝试获取它,这样当我按下一个按钮时,它就会显示在输入框中。例如,如果我按2按钮,然后按+按钮,然后按3按钮,我希望它显示在程序顶部的输入框中

import tkinter
import tkinter.messagebox

class myGUI:

    def __init__(self):
        self.main_window = tkinter.Tk()
        self.main_window.title('Calculator')

        ## Font
        button_font = ('Verdana', 15)
        entry_font = ('Verdana', 36)

        ## Define the frames
        self.frameZero = tkinter.Frame(self.main_window)
        self.frameOne = tkinter.Frame(self.main_window)
        self.frameTwo = tkinter.Frame(self.main_window)
        self.frameThree = tkinter.Frame(self.main_window)
        self.frameFour = tkinter.Frame(self.main_window)
        self.frameFive = tkinter.Frame(self.main_window)

        ## Define the buttons

        self.my_button1 = tkinter.Button(self.frameOne, text='1', command = lambda:btnClick(my_button1), height = 3, width = 7, font = button_font)
        self.my_button2 = tkinter.Button(self.frameOne, text='2', command = lambda:btnClick(my_button2), height = 3, width = 7, font = button_font)
        self.my_button3 = tkinter.Button(self.frameOne, text='3', command = lambda:btnClick(my_button3), height = 3, width = 7, font = button_font)
        self.my_buttonadd = tkinter.Button(self.frameOne, text='+', command = lambda:btnClick(my_buttonadd), height = 3, width = 7, font = button_font)
        self.my_button4 = tkinter.Button(self.frameTwo, text='4', command = lambda:btnClick(my_button4), height = 3, width = 7, font = button_font)
        self.my_button5 = tkinter.Button(self.frameTwo, text='5', command = lambda:btnClick(my_button5), height = 3, width = 7, font = button_font)
        self.my_button6 = tkinter.Button(self.frameTwo, text='6', command = lambda:btnClick(my_button6), height = 3, width = 7, font = button_font)
        self.my_buttonsub = tkinter.Button(self.frameTwo, text='-', command = lambda:btnClick(my_buttonsub), height = 3, width = 7, font = button_font)
        self.my_button7 = tkinter.Button(self.frameThree, text='7', command = lambda:btnClick(my_button7), height = 3, width = 7, font = button_font)
        self.my_button8 = tkinter.Button(self.frameThree, text='8', command = lambda:btnClick(my_button8), height = 3, width = 7, font = button_font)
        self.my_button9 = tkinter.Button(self.frameThree, text='9', command = lambda:btnClick(my_button9), height = 3, width = 7, font = button_font)
        self.my_buttonmult = tkinter.Button(self.frameThree, text='*', command = lambda:btnClick(my_buttonmult), height = 3, width = 7, font = button_font)
        self.my_buttonC = tkinter.Button(self.frameFour, text='C', height = 3, width = 7, font = button_font)
        self.my_button0 = tkinter.Button(self.frameFour, text='0', command = lambda:btnClick(my_button0), height = 3, width = 7, font = button_font)
        self.my_buttonperiod = tkinter.Button(self.frameFour, text='.', command = lambda:btnClick(my_buttonperiod), height = 3, width = 7, font = button_font)
        self.my_buttondiv = tkinter.Button(self.frameFour, text='/', command = lambda:btnClick(my_buttondiv), height = 3, width = 7, font = button_font)
        self.my_buttoncalc = tkinter.Button(self.frameFive, text='Calculate', height = 4, width = 30, font = button_font)

        ## Define the entry area
        self.my_entry = tkinter.Entry(self.frameZero, width = 13, font = entry_font)

        ## Pack the entry area
        self.my_entry.pack(side = 'left')

        ## Pack the buttons
        self.my_button1.pack(side = 'left')
        self.my_button2.pack(side = 'left')
        self.my_button3.pack(side = 'left')
        self.my_buttonadd.pack(side = 'left')
        self.my_button4.pack(side = 'left')
        self.my_button5.pack(side = 'left')
        self.my_button6.pack(side = 'left')
        self.my_buttonsub.pack(side = 'left')
        self.my_button7.pack(side = 'left')
        self.my_button8.pack(side = 'left')
        self.my_button9.pack(side = 'left')
        self.my_buttonmult.pack(side = 'left')
        self.my_buttonC.pack(side = 'left')
        self.my_button0.pack(side = 'left')
        self.my_buttonperiod.pack(side = 'left')
        self.my_buttondiv.pack(side = 'left')
        self.my_buttoncalc.pack(side = 'left')

        ## Pack the frames
        self.frameZero.pack()
        self.frameOne.pack()
        self.frameTwo.pack()
        self.frameThree.pack()
        self.frameFour.pack()
        self.frameFive.pack()

        ## Click button function
        def btnClick(numbers):
            global operator
            operator=operator + str(numbers)
            my_entry = self.set(operator)


        tkinter.mainloop()

my_gui = myGUI()

您需要实现一个
tk.StringVar()
,它可以将值存储在计算器屏幕的顶部。了解此变量的主要方法是
.get()
.set()
,这就是访问值(
.get()
)或更新值(
.set()
)的方式。您必须将其链接到带有
textvariable
参数的条目。设置好后,我将
btnClick()
函数的输入都更改为字符串。一旦获得了所需的字符串(例如“12+5”),就可以使用python内置的
eval()
函数来计算结果!注意,此方法不安全,因为用户可能会使用它执行任意代码(黑客攻击您的程序),具体取决于您如何实现它!代码的工作版本如下所示:

import tkinter
import tkinter.messagebox

class myGUI:

    def __init__(self):
        self.main_window = tkinter.Tk()
        self.main_window.title('Calculator')

        ## Font
        button_font = ('Verdana', 15)
        entry_font = ('Verdana', 36)

        ## Define the frames
        self.frameZero = tkinter.Frame(self.main_window)
        self.frameOne = tkinter.Frame(self.main_window)
        self.frameTwo = tkinter.Frame(self.main_window)
        self.frameThree = tkinter.Frame(self.main_window)
        self.frameFour = tkinter.Frame(self.main_window)
        self.frameFive = tkinter.Frame(self.main_window)

        ## Define the entry area
        self.entry_variable = tkinter.StringVar()
        self.my_entry = tkinter.Entry(self.frameZero, textvariable=self.entry_variable, width=13, font=entry_font)

        ## Define the buttons

        self.my_button1 = tkinter.Button(self.frameOne, text='1', command = lambda:btnClick("1"), height = 3, width = 7, font = button_font)
        self.my_button2 = tkinter.Button(self.frameOne, text='2', command = lambda:btnClick("2"), height = 3, width = 7, font = button_font)
        self.my_button3 = tkinter.Button(self.frameOne, text='3', command = lambda:btnClick("3"), height = 3, width = 7, font = button_font)
        self.my_buttonadd = tkinter.Button(self.frameOne, text='+', command = lambda:btnClick("+"), height = 3, width = 7, font = button_font)
        self.my_button4 = tkinter.Button(self.frameTwo, text='4', command = lambda:btnClick("4"), height = 3, width = 7, font = button_font)
        self.my_button5 = tkinter.Button(self.frameTwo, text='5', command = lambda:btnClick("5"), height = 3, width = 7, font = button_font)
        self.my_button6 = tkinter.Button(self.frameTwo, text='6', command = lambda:btnClick("6"), height = 3, width = 7, font = button_font)
        self.my_buttonsub = tkinter.Button(self.frameTwo, text='-', command = lambda:btnClick("-"), height = 3, width = 7, font = button_font)
        self.my_button7 = tkinter.Button(self.frameThree, text='7', command = lambda:btnClick("7"), height = 3, width = 7, font = button_font)
        self.my_button8 = tkinter.Button(self.frameThree, text='8', command = lambda:btnClick("8"), height = 3, width = 7, font = button_font)
        self.my_button9 = tkinter.Button(self.frameThree, text='9', command = lambda:btnClick("9"), height = 3, width = 7, font = button_font)
        self.my_buttonmult = tkinter.Button(self.frameThree, text='*', command = lambda:btnClick("*"), height = 3, width = 7, font = button_font)
        self.my_buttonC = tkinter.Button(self.frameFour, text='C', height = 3, width = 7, font = button_font)
        self.my_button0 = tkinter.Button(self.frameFour, text='0', command = lambda:btnClick("0"), height = 3, width = 7, font = button_font)
        self.my_buttonperiod = tkinter.Button(self.frameFour, text='.', command = lambda:btnClick("."), height = 3, width = 7, font = button_font)
        self.my_buttondiv = tkinter.Button(self.frameFour, text='/', command = lambda:btnClick("/"), height = 3, width = 7, font = button_font)
        self.my_buttoncalc = tkinter.Button(self.frameFive, text='Calculate', height = 4, width = 30, font = button_font, command=self.calculate)



        ## Pack the entry area
        self.my_entry.pack(side = 'left')

        ## Pack the buttons
        self.my_button1.pack(side = 'left')
        self.my_button2.pack(side = 'left')
        self.my_button3.pack(side = 'left')
        self.my_buttonadd.pack(side = 'left')
        self.my_button4.pack(side = 'left')
        self.my_button5.pack(side = 'left')
        self.my_button6.pack(side = 'left')
        self.my_buttonsub.pack(side = 'left')
        self.my_button7.pack(side = 'left')
        self.my_button8.pack(side = 'left')
        self.my_button9.pack(side = 'left')
        self.my_buttonmult.pack(side = 'left')
        self.my_buttonC.pack(side = 'left')
        self.my_button0.pack(side = 'left')
        self.my_buttonperiod.pack(side = 'left')
        self.my_buttondiv.pack(side = 'left')
        self.my_buttoncalc.pack(side = 'left')

        ## Pack the frames
        self.frameZero.pack()
        self.frameOne.pack()
        self.frameTwo.pack()
        self.frameThree.pack()
        self.frameFour.pack()
        self.frameFive.pack()



        ## Click button function
        def btnClick(number_clicked):
            self.entry_variable.set("{}{}".format(self.entry_variable.get(), number_clicked))

    def calculate(self):
        print("{}".format(eval(str(self.entry_variable.get()))))
        self.entry_variable.set("{}".format(str(eval(str(self.entry_variable.get())))))


my_gui = myGUI()
my_gui.main_window.mainloop() # this should NOT be inside your class like you had it...

总的来说,我喜欢你的框架布局和整体设计。Lambda函数也很好!欢迎使用StackOverflow。

如果他使用
for
循环来创建这些小部件,可能会更好。这样做会使工作变得更简单,占用的空间也更小。@Saad也许吧。但我认为这需要对代码进行重大的重组,我在这里尽量不这样做。OP的问题不是如何将它们放入
for
循环中,因此这样做对我来说将是更多的工作,而使用这几个小部件对我来说真的不是什么大问题。它不像成千上万甚至上百个小部件被制造出来,只是十几个左右。OP的问题与实现
tk.StringVar()
直接相关,正如我在这里所展示的那样。如果您想展示如何使用循环,我建议您发布一个解决方案,可以做到这一点!我会投票的!你是对的,我必须再次制作Gui,因为OP甚至还没有继承
Tk()
,这与OP的方式大不相同,OP的方式超出了他的理解,或者我必须对答案进行大量解释。