簡體   English   中英

如何在tkinter(Python)中增加輸入(顯示)框的大小(高度)?

[英]How do you increase the size(height) of the entry(display) box in tkinter(Python)?

我一直在編碼,我無法找到如何增加計算器上顯示框的大小(高度)。 我做了很多嘗試,除了它給了我一個錯誤。 有人可以解釋我該怎么做,以便現在可以增加顯示器的高度,因為它現在太小了。 所以請幫忙。

(而且我正在使用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-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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM