如何在tkinter中拉伸条目以适合屏幕?

时间:2019-01-20 17:17:46

标签: python tkinter

我知道您可以使用label.pack(fill=x)来做到这一点,但是我需要使用grid。我四处搜索,发现root.grid_rowconfigure(0, weight=1)root.grid_columnconfigure(0,weight=1),但是它们只是将条目居中在屏幕中间,因此我也需要更改大小。我是新来的,因此不胜感激。

from tkinter import *

import pyperclip

root = Tk()

def listify(userString, brackets=True, quotes=True):

    outputList = []
    outputString = ''

    if brackets == True:
        outputList.append('[')

    if quotes == True:
        outputList.append("\"")
    userString = str(userString)
    lastCharacter = ""

    for character in userString:

        if character == " " or character == '   ':

            if lastCharacter == " " or lastCharacter == '   ':
                pass

            elif lastCharacter != " " or lastCharacter != '   ':

                if quotes == True:
                    outputList.append("\"")

                outputList.append(', ')

                if quotes == True:
                    outputList.append("\"")

        else:
            outputList.append(character)

        lastCharacter = character

    if quotes == True:
        outputList.append("\"")
    if brackets == True:
        outputList.append("]")

    outputString = outputString.join(outputList)
    return(outputString)

def mainListify():

    outputLabel.destroy()
    pyperclip.copy(listify(userText.get(),bracketsVar.get(),quotesVar.get()))
    newLabel = Label(root, text = listify(userText.get(),bracketsVar.get(),quotesVar.get()))
    newLabel.grid()


userText = StringVar()
quotesVar = BooleanVar()
bracketsVar = BooleanVar()

quotesVar.set(True)
bracketsVar.set(True)

userEntry = Entry(root, textvariable=userText)
quotes = Checkbutton(root, text='Quotes', onvalue=True, offvalue=False, variable=quotesVar)
brackets = Checkbutton(root, text='Brackets', onvalue=True, offvalue=False, variable=bracketsVar)
startButton = Button(root, text='Start', command=mainListify)
outputLabel = Label(root, text='Output Will Show Here')


userEntry.grid(columnspan=2)
quotes.grid(row=1)
brackets.grid(row=1,column=1)
outputLabel.grid(row=3,column=1)
startButton.grid(row=2, column=0)

root.mainloop()

1 个答案:

答案 0 :(得分:1)

您需要使用sticky grid 参数,该参数定义了 how to expand the widget if the resulting cell is larger than the widget itself

这是一个示例(剥离了您的代码):

from tkinter import *

root = Tk()

userText = StringVar()
quotesVar = BooleanVar()
bracketsVar = BooleanVar()

quotesVar.set(True)
bracketsVar.set(True)

userEntry = Entry(root, textvariable=userText)
quotes = Checkbutton(root, text='Quotes', onvalue=True, offvalue=False, variable=quotesVar)
brackets = Checkbutton(root, text='Brackets', onvalue=True, offvalue=False, variable=bracketsVar)
startButton = Button(root, text='Start')
outputLabel = Label(root, text='Output Will Show Here')

userEntry.grid(row=0, column=0, columnspan=2, sticky="nsew")
quotes.grid(row=1, column=0, sticky="ew", padx=10)# padx for horizontal spacing
brackets.grid(row=1, column=1, sticky="ew", padx=10)
outputLabel.grid(row=3, column=0, columnspan=2)
startButton.grid(row=2, column=0, columnspan=2)

root.mainloop()

结果:

Result