如何在Python中更改Tkinter文本框的宽度和高度?

时间:2017-06-09 23:17:37

标签: python button tkinter calculator

所以我一直在使用Tkinter研究Python上的计算器。但是,我想将按钮分开,而不是将它们直接放在一起。我不使用root.Frame函数。这是我的代码:

from Tkinter import *

master = Tk()
display = Entry(master, width=46, justify='right', bd=1, bg='#eee5de', relief=RIDGE)

master.title("Calculator | Coded by Mathieu")


# ~Class~ #


class Calculator:
    def __init__(self):
        self.var1 = ""
        self.var2 = ""
        self.result = 0
        self.current = 0
        self.operator = 0

    def numb_butt(self, index):
        if self.current is 0:
            self.var1 = str(self.var1) + str(index)
            display.delete(0, END)
            display.insert(0, string=self.var1)
        else:
            self.var2 = str(self.var2) + str(index)
            display.delete(0, END)
            display.insert(0, string=self.var2)

    def equate(self):
        if self.operator is 0:
            self.result = float(self.var1) + float(self.var2)
        elif self.operator is 1:
            self.result = float(self.var1) - float(self.var2)
        elif self.operator is 2:
            self.result = float(self.var1) * float(self.var2)
        elif self.operator is 3:
            self.result = float(self.var1) / float(self.var2)
        display.delete(0, END)
        display.insert(0, string=self.result)

    def set_op(self, op):
        self.operator = op
        display.delete(0, END)
        if self.current is 0:
            self.current = 1
        else:
            self.equate()
            self.var2 = ""

    def clear(self):
        self.__init__()
        display.delete(0, END)


# ~Buttons~ #

calc = Calculator()

b0 = Button(master, text="0", command=lambda: calc.numb_butt(0), width=12, height=3, bd=2, relief=RAISED)
b1 = Button(master, text="1", command=lambda: calc.numb_butt(1), width=12, height=3, bd=2, relief=RAISED)
b2 = Button(master, text="2", command=lambda: calc.numb_butt(2), width=12, height=3, bd=2, relief=RAISED)
b3 = Button(master, text="3", command=lambda: calc.numb_butt(3), width=12, height=3, bd=2, relief=RAISED)
b4 = Button(master, text="4", command=lambda: calc.numb_butt(4), width=12, height=3, bd=2, relief=RAISED)
b5 = Button(master, text="5", command=lambda: calc.numb_butt(5), width=12, height=3, bd=2, relief=RAISED)
b6 = Button(master, text="6", command=lambda: calc.numb_butt(6), width=12, height=3, bd=2, relief=RAISED)
b7 = Button(master, text="7", command=lambda: calc.numb_butt(7), width=12, height=3, bd=2, relief=RAISED)
b8 = Button(master, text="8", command=lambda: calc.numb_butt(8), width=12, height=3, bd=2, relief=RAISED)
b9 = Button(master, text="9", command=lambda: calc.numb_butt(9), width=12, height=3, bd=2, relief=RAISED)
b_dot = Button(master, text=".", command=lambda: calc.numb_butt("."), width=12, height=3, bd=2, relief=RAISED)

plus = Button(master, text="+", command=lambda: calc.set_op(0), width=5, height=3, bd=2, relief=RAISED)
minus = Button(master, text="-", command=lambda: calc.set_op(1), width=5, height=3, bd=2, relief=RAISED)
times = Button(master, text="*", command=lambda: calc.set_op(2), width=5, height=3, bd=2, relief=RAISED)
divide = Button(master, text="/", command=lambda: calc.set_op(3), width=5, height=3, bd=2, relief=RAISED)

equals = Button(master, text="=", command=calc.equate, width=5, bd=2, relief=RAISED)
clear = Button(master, text="C", command=calc.clear, width=12, height=3, bd=2, relief=RAISED)

# ~Positioning~ #

display.place(x=0, y=2)
b7.grid(row=2, column=0)
b8.grid(row=2, column=1)
b9.grid(row=2, column=2)
b4.grid(row=3, column=0)
b5.grid(row=3, column=1)
b6.grid(row=3, column=2)
b1.grid(row=4, column=0)
b2.grid(row=4, column=1)
b3.grid(row=4, column=2)
b0.grid(row=5, column=0)

b_dot.grid(row=5, column=1)
clear.grid(row=5, column=2)

plus.grid(row=2, column=3)
minus.grid(row=3, column=3)
times.grid(row=4, column=3)
divide.grid(row=5, column=3)
equals.grid(row=1, column=3)

master.mainloop()

1 个答案:

答案 0 :(得分:0)

对于您的问题,有几个选择。

最明显和最简单的解决方案是使用padxpady配置选项。

对于您的每个行,只需添加填充即可放置小部件:

拿这个:

b7.grid(row=2, column=0)

并添加padx,pady:

b7.grid(row=2, column=0, padx = 5, pady = 5)

另一种选择是使用place方法将每个按钮专门放置几个像素空间彼此分开,从而提供所需的空间。然而,这不是首选方法,因为它将使代码在将来更难维护。

而不是.grid()使用.place()并使用参数xy: 这个选项的维护要多得多,应该避免使用。

你可以使用.pack()填充配置,但我认为在这里使用.grid()是最好的,你应该坚持下去。

使用以下内容获得相同的布局,但使用填充。

注意:我将您的号码更改为使用网格而不是地点。您需要做的就是定义列范围,这样就不会改变其他小部件的位置。

display.grid(row = 1, column = 0, columnspan = 3, padx = 5, pady = 5)
b7.grid(row=2, column=0, padx = 5, pady = 5)
b8.grid(row=2, column=1, padx = 5, pady = 5)
b9.grid(row=2, column=2, padx = 5, pady = 5)
b4.grid(row=3, column=0, padx = 5, pady = 5)
b5.grid(row=3, column=1, padx = 5, pady = 5)
b6.grid(row=3, column=2, padx = 5, pady = 5)
b1.grid(row=4, column=0, padx = 5, pady = 5)
b2.grid(row=4, column=1, padx = 5, pady = 5)
b3.grid(row=4, column=2, padx = 5, pady = 5)
b0.grid(row=5, column=0, padx = 5, pady = 5)

b_dot.grid(row=5, column=1, padx = 5, pady = 5)
clear.grid(row=5, column=2, padx = 5, pady = 5)

plus.grid(row=2, column=3, padx = 5, pady = 5)
minus.grid(row=3, column=3, padx = 5, pady = 5)
times.grid(row=4, column=3, padx = 5, pady = 5)
divide.grid(row=5, column=3, padx = 5, pady = 5)
equals.grid(row=1, column=3, padx = 5, pady = 5)