如何使 tkinter.Entry 延伸到 window 的右侧?

How do I make a tkinter.Entry extend to the right hand side of the window?

我正在 python 3 中使用 tkinter 创建一个密码游戏,它将标题、短语、赠品字母的数量以及赠品字母本身作为输入。

window 目前看起来是这样的: tkinter inputs window

问题是,我希望白色输入框从左侧标签的末尾开始,一直延伸到 window 的末尾。

像这样: desired input window design

我目前正在使用 .pack() 方法将 objects 放置在 window 中,包含行和列。

一共4行2列:

第 1 行:标题标签、标题条目

第 2 行:phraseLabel,phraseEntry

第 3 行:numGiveawayLettersLabel,numGiveawayLettersEntry

第 4 行:giveawayLettersLabel,(不确定这是什么,但它会包含赠品字母)

我的代码:

global windowBackgroundColour
windowBackgroundColour = "#5A9AD7"

def createUserInputsWindow():
    global userInputsWindow
    global windowBackgroundColour
    # specifies what global variables tihs method is using

    # -------------------- Creates User Inputs Window --------------------

    userInputsWindow = tkinter.Tk()
    userInputsWindow.configure(bg=windowBackgroundColour)
    userInputsWindow.title("The Puzzle Club")

    # -------------------- Creates Frames (Containers) --------------------

    userInputsWindowRow1 = tkinter.Frame(userInputsWindow)
    userInputsWindowRow1Column1 = tkinter.Frame(userInputsWindowRow1)
    userInputsWindowRow1Column2 = tkinter.Frame(userInputsWindowRow1)

    userInputsWindowRow2 = tkinter.Frame(userInputsWindow)
    userInputsWindowRow2Column1 = tkinter.Frame(userInputsWindowRow2)
    userInputsWindowRow2Column2 = tkinter.Frame(userInputsWindowRow2)

    userInputsWindowRow3 = tkinter.Frame(userInputsWindow)
    userInputsWindowRow3Column1 = tkinter.Frame(userInputsWindowRow3)
    userInputsWindowRow3Column2 = tkinter.Frame(userInputsWindowRow3)

    userInputsWindowRow4 = tkinter.Frame(userInputsWindow)
    userInputsWindowRow4Column1 = tkinter.Frame(userInputsWindowRow4)
    userInputsWindowRow4Column2 = tkinter.Frame(userInputsWindowRow4)

    # -------------------- Populating Frames (Containers) --------------------

    userInputsWindowRow1.pack(side="top", fill="both")
    userInputsWindowRow1Column1.pack(side="left", fill="both")
    userInputsWindowRow1Column2.pack(side="right", fill="both")

    userInputsWindowRow2.pack(side="top", fill="both")
    userInputsWindowRow2Column1.pack(side="left", fill="both")
    userInputsWindowRow2Column2.pack(side="right", fill="both")

    userInputsWindowRow3.pack(side="top", fill="both")
    userInputsWindowRow3Column1.pack(side="left", fill="both")
    userInputsWindowRow3Column2.pack(side="right", fill="both")

    userInputsWindowRow4.pack(side="top", fill="both")
    userInputsWindowRow4Column1.pack(side="left", fill="both")
    userInputsWindowRow4Column2.pack(side="right", fill="both")

    userInputsWindowRow1.configure(bg=userInputsWindow.cget("bg"))
    userInputsWindowRow2.configure(bg=userInputsWindow.cget("bg"))
    userInputsWindowRow3.configure(bg=userInputsWindow.cget("bg"))
    userInputsWindowRow4.configure(bg=userInputsWindow.cget("bg"))

    userInputsWindow.resizable(width=False, height=False)



    # -------------------- Creating Widgets --------------------

    titleLabel = tkinter.Label(userInputsWindowRow1Column1, text="Title:")
    titleLabel.configure(fg="black", bg=userInputsWindowRow1.cget("bg"), font="none 32")
    # This creates a title label, that has the text - "Title:"

    phraseLabel = tkinter.Label(userInputsWindowRow2Column1, text="Phrase:")
    phraseLabel.configure(fg="black", bg=userInputsWindowRow2.cget("bg"), font="none 32")
    # This crates a phrase label, that has the text - "Phrase:"

    numGiveawayLettersLabel = tkinter.Label(userInputsWindowRow3Column1, text="# of Giveaway Letters:")
    numGiveawayLettersLabel.configure(fg="black", bg=userInputsWindowRow3.cget("bg"), font="none 32")
    # This creates a label that has the text - "# of Giveaway Letters:"

    giveawayLettersLabel = tkinter.Label(userInputsWindowRow4Column1, text="Giveaway Letters:")
    giveawayLettersLabel.configure(fg="black", bg=userInputsWindowRow4.cget("bg"), font="none 32")
    # This creates a label that has the text - "Giveaway Letters:"

    titleEntry = tkinter.Entry(userInputsWindowRow1Column2, font=titleLabel.cget("font"))
    titleEntry.insert(0, "Enter title")
    # This creates an entry box for the user to enter the title of their puzzle in

    phraseEntry = tkinter.Entry(userInputsWindowRow2Column2, font=phraseLabel.cget("font"))
    phraseEntry.insert(0, "Enter Phrase")
    # This creates an entry box for the user to enter the phrase they wish to use in the puzzle

    numGiveawayLettersEntry = tkinter.Entry(userInputsWindowRow3Column2, font=numGiveawayLettersLabel.cget("font"))
    numGiveawayLettersEntry.insert(0, "(1 - 26)")
    # This creates an entry box for the user to enter how many giveaway letters they wish to have

    # -------------------- Populating Widgets --------------------

    titleLabel.pack(side="left", fill="both")
    phraseLabel.pack(side="left", fill="both")
    numGiveawayLettersLabel.pack(side="left", fill="both")
    giveawayLettersLabel.pack(side="left", fill="both")

    titleEntry.pack(side="left", fill="both", expand=True)
    phraseEntry.pack(side="left", fill="both", expand=True)
    numGiveawayLettersEntry.pack(side="left", fill="both", expand=True)

    return

不胜感激,谢谢!

(我的代码中有很多函数,上面的代码中可能没有引用一些全局变量,如果需要完整代码,请给我留言)

最简单的方法是每行使用一帧。您已经在这样做了,但是如果您使用函数或 class 会容易得多。您唯一缺少的是您需要对每个条目和每个帧都使用 fill 选项。

使用 class 的示例:

class InputRow(tkinter.Frame):
    def __init__(self, master, text, help_text):
        tkinter.Frame.__init__(self, master, background=master.cget("background"))
        self.label = tkinter.Label(self, text=text, background=master.cget("background"))
        self.entry = tkinter.Entry(self)
        self.label.pack(side="left")
        self.entry.pack(side="left", fill="x", expand=True)
        self.entry.insert(0, help_text)

    def get(self):
        return self.entry.get()

然后您可以像这样创建标签和条目:

rows = [
    InputRow(userInputsWindow, "Title:", "Enter title"),
    InputRow(userInputsWindow, "Phrase:", "Enter Phrase"),
    InputRow(userInputsWindow, "# of Giveaway Letters:", "(1-26)"),
    InputRow(userInputsWindow, "Giveaway Letters:", ""),
]

for row in rows:
    row.pack(side="top", fill="x")