简体   繁体   English

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

[英]How do you stretch an entry to fit screen in tkinter?

I know that you can do it with label.pack(fill=x) , but I need to use grid. 我知道您可以使用label.pack(fill=x)做到这label.pack(fill=x) ,但我需要使用grid。 I searched around and found root.grid_rowconfigure(0, weight=1) and root.grid_columnconfigure(0,weight=1) , but they just center the entry in the middle of the screen, and I need the size to change too. 我四处搜索,发现root.grid_rowconfigure(0, weight=1)root.grid_columnconfigure(0,weight=1) ,但它们只是将条目居中放在屏幕中间,我也需要更改大小。 I am new to this so any help is greatly appreciated. 我对此并不陌生,因此不胜感激。

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

You need to use the sticky parameter of grid , which defines how to expand the widget if the resulting cell is larger than the widget itself . 您需要使用gridsticky参数,该参数定义how to expand the widget if the resulting cell is larger than the widget itself

Here is an example (stripped your code): 这是一个示例(剥离了您的代码):

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 : 结果:

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM