用tkinter创建按钮

时间:2019-01-21 16:06:38

标签: python button tkinter

我真的很好奇为什么我无法add_button上班, 因为创建时窗口无法显示。

from tkinter import *
class Calculator:

#-------------------------------------------------               
    def __init__(self, master):

        self.master = master 

        master.title("Calculator")

        self.close_button = Button(master, text = "Close", command = master.destroy)        

        Label(master, text = "First Digit").grid(row = 0)
        Label(master, text = "Second Digit").grid(row = 1)

        self.input1 = 0
        self.input2 = 0

        input1 = Entry(master)
        input2 = Entry(master)

        input1.grid(row = 0, column = 1)
        input2.grid(row = 1, column = 1)


        self.close_button.grid(row = 2, column = 0)

        self.add_buton = Button(master, text = "Add", command = self.add())
        self.add_button.grid(row = 2, column = 1)                              

        master.configure(background = 'grey')

        return 

#-------------------------------------------------

    def add(self):
            return self.input1.get() + self.input2.get()   

#-------------------------------------------------



#-------------------------------------------------
root = Tk()
calc = Calculator(root)
root.mainloop()
#-------------------------------------------------

2 个答案:

答案 0 :(得分:1)

欢迎叠放!

我已经仔细阅读了您的代码,能够执行您要问的问题。您的代码中存在一些错误:

a)您有self.add_butonself.add_button导致了错误。

b)不需要self.input1 = 0self.input2 = 0

c)您正在调用self.add()作为命令,因此您应该调用self.add。将其作为命令调用时,不需要()

d)input1 = Entry(master)应该是self.input1 = tk.Entry(master)

e)您应该将输入值转换为intfloat,否则它将只是一个值到达另一个值的末尾。 (例如1 + 5 = 15int(1) + int(5) = 6

这是您的代码,其中的输入框应正常工作。我有import tkinter as tk,因此为什么是tk.Entry

from tkinter import *
import tkinter as tk
class Calculator:

#-------------------------------------------------               
    def __init__(self, master):

        self.master = master 

        master.title("Calculator")

        self.close_button = Button(master, text = "Close", command = master.destroy)        

        Label(master, text = "First Digit").grid(row = 0)
        Label(master, text = "Second Digit").grid(row = 1)

        self.input1 = tk.Entry(bd=5, width=35, background='gray35', foreground='snow')
        self.input2 = tk.Entry(bd=5, width=35, background='gray35', foreground='snow')

        self.input1.grid(row = 0, column = 1)
        self.input2.grid(row = 1, column = 1)


        self.close_button.grid(row = 2, column = 0)

        self.add_button = tk.Button(master, text = "Add", command = self.add)
        self.add_button.grid(row = 2, column = 1)                              

        master.configure(background = 'grey')

        return 

#-------------------------------------------------

    def add(self):
        val = self.input1.get()
        print(val)

#-------------------------------------------------



#-------------------------------------------------
root = Tk()
calc = Calculator(root)
root.mainloop()

这现在应该也可以按照您想要的方式工作。条目中的变量可以更改为适合。使用self.input1.get()调用条目的值是正确的。

希望这有所帮助。

答案 1 :(得分:0)

尝试一下:

from tkinter import Button, Entry, Label, Tk


class Calculator:

    """DOCSTRING"""

    def __init__(self, master):
        master.title("Calculator")
        master.configure(background='grey')

        button = dict()
        button['master'] = master
        button['text'] = 'Close'
        button['command'] = master.destroy
        close_button = Button(**button)

        Label(master, text='First Digit').grid(row=0)
        Label(master, text='Second Digit').grid(row=1)

        entry = dict()
        entry['bd'] = 5
        entry['width'] = 35
        entry['background'] = 'gray35'
        entry['foreground'] = 'snow'
        self.input1 = Entry(**entry)
        self.input2 = Entry(**entry)

        self.input1.grid(row=0, column=1)
        self.input2.grid(row=1, column=1)
        close_button.grid(row=2, column=0)
        add_button = Button(master, text="Add", command=self.add)
        add_button.grid(row=2, column=1)


    def add(self):
        val = int(self.input1.get()) + int(self.input2.get())
        print(val)


root = Tk()
calc = Calculator(root)
root.mainloop()