简体   繁体   English

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

[英]How do you change the width and height of a Tkinter text box in Python?

So I've been working on a calculator on Python using Tkinter. 因此,我一直在使用Tkinter在Python上开发计算器。 However, I'd like to space the buttons out, not putting them directly next to each other. 但是,我想将按钮隔开,而不是将它们直接相邻放置。 I don't use the root.Frame function. 我不使用root.Frame函数。 Here is my code: 这是我的代码:

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()

For your question there are a few options. 对于您的问题,有一些选择。

The most obvious and simple solution is to use padx and pady configuration options. 最明显和最简单的解决方案是使用padxpady配置选项。

for each of your lines you place your widgets with just add padding: 对于每行,只需添加填充即可放置小部件:

take this: 拿着它:

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

and add padx, pady: 并添加padx,pady:

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

Another option is to use the place method to specifically place each button several pixel spaces apart from each other giving the space you want. 另一个选择是使用place方法将每个按钮专门放置在彼此分开几个像素空间的位置,以提供所需的空间。 However this is not the preferred method as it will make the code a bit harder to maintain in the future. 但是,这不是首选方法,因为它将使将来的代码维护变得更加困难。

instead of .grid() use .place() and use the parameters x and y : This option is much more maintenance heavy and should be avoided. 代替.grid()使用.place()并使用参数xy :此选项的维护.place()大得多,应避免使用。

You could use .pack() with padding configurations but I think using .grid() here is best and you should stick with that. 您可以将.pack()与padding配置一起使用,但是我认为最好在此处使用.grid() ,并且应该坚持这样做。

Use the following to get the the same layout but with padding. 使用以下命令获得相同的布局,但使用填充。

Note: I changed your number filed to use grid and not place. 注意:我已将您申请的号码更改为使用网格而不是放置。 All you need to do is define the column span so it does not change the placement of the other widgets. 您需要做的就是定义列跨度,这样它就不会更改其他小部件的位置。

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)

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

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