python - 如何增加 tkinter(Python) 中输入(显示)框的大小(高度)?

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

"

最佳答案

使用 grid 怎么样?而不是place ?如下

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

正如使用 python 内置命令时读取的那样 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

关于python - 如何增加 tkinter(Python) 中输入(显示)框的大小(高度)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48330657/

相关文章:

python - 直到给定百分位数的变量累积和

python - 从没有循环的多级 Pandas 数据框中删除行列表

python - 循环遍历 pickle 读取的列表以查找 userid

python - 抽象基类不强制执行函数实现

google-app-engine - 如何为GAE数据存储区查询创建动态变量列表?

exec 中的 python 字典理解使用全局变量而不是局部变量

python - 关于小部件="selection"的说明

python - Luigi 工作流中的 MySQL 目标

python-3.x - 属性错误: 'module' object has no attribute 'packages'

python-3.x - 是否有与 Stream.findAny() 等效的 Python?