简体   繁体   中英

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. 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. 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.

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:

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. 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.

You could use .pack() with padding configurations but I think using .grid() here is best and you should stick with that.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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