简体   繁体   English

用tkinter创建按钮

[英]creating button with tkinter

I was really curious why I cannot get my add_button to work, as the window fails to come up when creating it. 我真的很好奇为什么我无法使我的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()
#-------------------------------------------------

Welcome to Stack! 欢迎来到Stack!

I've looked through you code I've been able to do what you are asking. 我仔细阅读了您的代码,能够完成您所要求的。 There were a few errors within your code: 您的代码中存在一些错误:

a) you had self.add_buton and self.add_button which caused an error. a)您有self.add_butonself.add_button导致错误。

b) self.input1 = 0 and self.input2 = 0 are not required. b) self.input1 = 0self.input2 = 0

c) You were calling self.add() as the command and you should be calling self.add . c)您正在调用self.add()作为命令,并且您应该正在调用self.add When calling it as a command you do not need () 作为命令调用时,您不需要()

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

e) You should convert your input values into int or float as otherwise it will just one value onto the end of the other. e)您应该将输入值转换为intfloat ,否则它将仅将一个值放在另一个值的末尾。 (Eg, 1 + 5 = 15 whereas int(1) + int(5) = 6 (例如1 + 5 = 15int(1) + int(5) = 6

Here is your code with the entry boxes working as they should. 这是您的代码,其中的输入框可以正常工作。 I have import tkinter as tk hence why it is tk.Entry 我已经import tkinter as tk tk.Entry 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()

This should now work how you wanted it too. 现在,它应该也可以按照您的要求工作。 The variables within the entry can be changed to suit. 条目中的变量可以更改为适合。 You were correct in calling the value of the entry with self.input1.get() . 您使用self.input1.get()调用条目的值是正确的。

Hope this has helped. 希望这有所帮助。

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

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