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

该窗口当前如下所示: tkinter输入窗口

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

像这样: 所需的输入窗口设计

我目前正在使用.pack()方法将对象(带有行和列)放置在窗口中。

总共有4行和两列:

第1行:titleLabel,titleEntry

第2列:短语标签,短语条目

第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

帮助将不胜感激,谢谢!

(我的代码中有很多函数,并且上面的代码中可能未引用某些全局变量,如果您需要完整的代码,请给我发消息)

分析解答

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

使用类的示例:

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