如何更改Tkinter输入字段的宽度或高度

时间:2018-06-25 13:02:23

标签: python tkinter

我一直在编写用于检查语法的脚本。现在,我已经使用Tkinter将其更新为gui。问题是我试图指出语法错误的行,并且当我使用输入字段输入文本时,所有内容都在一行中。

我的问题是如何扩展输入字段?

这是我的代码:

import re
from tkinter import *

window = Tk()
window.minsize(width=300, height= 20)
wr = []

def work():
    x = e1.get()
    print(x)
    BigLetterSearcher = re.compile(r'\. .|\n')
    mo = BigLetterSearcher.findall(e1.get())
    x = 1
    y = 0
    v = 0
    z = ""
    wr = []
    for i in mo:
        if i == '\n':
            x += 1
        elif i != i.upper():
            v = 1
            if x != y:
                z = "Row", x
                wr.append(z)
            wr.append(i)
            y = x
    if v == 0:
        wr.append ("Congratulations none of your grammar was wrong!")
    l1.configure(text=wr)

l1 = Label(window, text="example")
e1 = Entry(window, text="Enter text here: ")
b1 = Button(window, text="Work", command=work)

leb = [l1, e1, b1]
for all in leb:
    all.pack()

window.mainloop()

2 个答案:

答案 0 :(得分:2)

输入小部件无法垂直扩展。这是因为已经有一个为此设计的小部件,并且称为Text()。要将文本添加到文本小部件中,我们可以使用insert(),并使用两部分索引指定位置。第一部分是行,第二部分是列。对于行/行,它从数字1开始,对于该行的索引,它从零开始。

例如,如果您希望在第一行/列中插入一些内容,则可以执行insert("1.0", "some data here")

在这里,您是使用Text()进行编码的。

import re
from tkinter import *

window = Tk()
window.minsize(width=300, height= 20)
wr = []

def work():
    x = e1.get("1.0", "end-1c")
    print(x)
    BigLetterSearcher = re.compile(r'\. .|\n')
    mo = BigLetterSearcher.findall(x)
    x = 1
    y = 0
    v = 0
    z = ""
    wr = []
    for i in mo:
        if i == '\n':
            x += 1
        elif i != i.upper():
            v = 1
            if x != y:
                z = "Row", x
                wr.append(z)
            wr.append(i)
            y = x
    if v == 0:
        wr.append ("Congratulations none of your grammar was wrong!")
    l1.configure(text=wr)

l1 = Label(window, text="example")
e1 = Text(window, width=20, height=3)
e1.insert("end", "Enter text here: ")
b1 = Button(window, text="Work", command=work)

leb = [l1, e1, b1]
for all in leb:
    all.pack()

window.mainloop()

答案 1 :(得分:0)

只能通过更改与“输入”字段关联的字体大小来垂直扩展“输入”字段...

e1 = Entry(window, text="Enter text here: ", font=('Ubuntu', 24))

结果输入字段比

e1 = Entry(window, text="Enter text here: ", font=('Ubuntu', 12))