我正在使用 python 3 中的 tkinter 创建一个密码游戏,它以标题、短语、赠品字母的数量和赠品字母本身作为输入。
窗口当前看起来像这样: tkinter 输入窗口
问题是,我希望白色输入框从左侧标签的末尾开始,一直延伸到窗口的末尾。
像这样: 想要的输入窗口设计
我目前正在使用该.pack()
方法在窗口中放置对象,包括行和列。
总共有 4 行和 2 列:
第 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
帮助将不胜感激,谢谢!
(我的代码中有很多函数,上面的代码中可能没有引用一些全局变量,如果需要完整代码,请给我留言)