如何在tkinter(Python)中增加条目(显示)框的大小(高度)?

时间:2018-01-18 21:32:53

标签: python python-3.x python-2.7 tkinter

我一直在编码,我无法找到如何增加计算器上显示框的大小(高度)。我尝试了很多东西,除了它给我一个错误。有人可以解释我应该做什么,所以我可以增加显示器的高度,因为现在它有点太小了。所以请帮助。

(我正在使用Windows(10)笔记本电脑。)

这是我的计算器的(示例)代码。 “

from tkinter import *

master = Tk()

display = Entry(master, width=46, justify='right', bd=0, bg='light grey')


master.title("Calculator")


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

    def numb_butt(self, index):
        if self.current is 0:
            self.var1 = str(self.var1) + str(index)
            display.delete(first=0, last=END)
            display.insert(0, string=self.var1)
        else:
            self.var2 = str(self.var2) + str(index)
            display.delete(first=0, last=END)
            display.insert(0, 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)
        display.delete(first=0, last=END)
        display.insert(0, self.result)
        self.var1 = 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)


calc = Calculator()

b0 = Button(master, text="0", command=lambda: calc.numb_butt(0), width=12,     
height=3)
b1 = Button(master, text="1", command=lambda: calc.numb_butt(1), width=12, 
height=3)
b2 = Button(master, text="2", command=lambda: calc.numb_butt(2), width=12, 
height=3)
b3 = Button(master, text="3", command=lambda: calc.numb_butt(3), width=12, 
height=3)
b4 = Button(master, text="4", command=lambda: calc.numb_butt(4), width=12, 
height=3)
b5 = Button(master, text="5", command=lambda: calc.numb_butt(5), width=12, 
height=3)
b6 = Button(master, text="6", command=lambda: calc.numb_butt(6), width=12, 
height=3)
b7 = Button(master, text="7", command=lambda: calc.numb_butt(7), width=12, 
height=3)
b8 = Button(master, text="8", command=lambda: calc.numb_butt(8), width=12, 
height=3)
b9 = Button(master, text="9", command=lambda: calc.numb_butt(9), width=12, 
height=3)
b_dot = Button(master, text=".", command=lambda: calc.numb_butt("."), 
width=12, height=3)
plus = Button(master, text="+", command=lambda: calc.set_op(0), width=12, 
height=3)
minus = Button(master, text="-", command=lambda: calc.set_op(1), width=12, 
height=3)
equals = Button(master, text="=", command=calc.equate, width=12, height=3)
clear = Button(master, text="C", command=calc.clear, width=12, height=3)

# *~* Positioning *~* #

display.place(x=0, y=0)

clear.grid(row=0, column=3)

b7.grid(row=4, column=0)
b8.grid(row=4, column=1)
b9.grid(row=4, column=2)

b4.grid(row=5, column=0)
b5.grid(row=5, column=1)
b6.grid(row=5, column=2)
minus.grid(row=5, column=3)

b1.grid(row=6, column=0)
b2.grid(row=6, column=1)
b3.grid(row=6, column=2)
plus.grid(row=6, column=3)

b0.grid(row=7, column=0)
b_dot.grid(row=7, column=1)
equals.grid(row=7, column=2)


master.mainloop()

1 个答案:

答案 0 :(得分:2)

使用grid代替place怎么样?如下

display.grid(row=0, columnspan=3, sticky=NSEW)

当使用python-builtin help函数时读取:

>>> help(display.grid)
Help on method grid_configure in module Tkinter:

grid_configure(self, cnf={}, **kw) method of Tkinter.Entry instance
    Position a widget in the parent widget in a grid. Use as options:
    column=number - use cell identified with given column (starting with 0)
    columnspan=number - this widget will span several columns
    in=master - use master to contain this widget
    in_=master - see 'in' option description
    ipadx=amount - add internal padding in x direction
    ipady=amount - add internal padding in y direction
    padx=amount - add padding in x direction
    pady=amount - add padding in y direction
    row=number - use cell identified with given row (starting with 0)
    rowspan=number - this widget will span several rows
    sticky=NSEW - if cell is larger on which sides will this
                  widget stick to the cell boundary