我一直在tkinter做一个游戏,为未来的项目练习我的编码技巧但是我遇到了一个绊脚石。我一直在阅读Tkinter菜单,我似乎找不到将它们放在窗口顶部以外的任何地方的方法。我想要做的是创建一个下拉选择框,允许您选择一个选项并根据该选项执行命令,但我希望它放在网格位置列中的标签旁边= 2行= 2。我想知道这是否可能,如果不是,那么做同样事情的另一种方式是什么。
import tkinter
import threading
import time
import math
from random import randrange
global Stage
Stage = 0
global Coins
Coins = 0
global Actions
Actions = 0
global ActionSpeed
ActionSpeed = 0
global Day
Day = 1
global Metal
Metal = "None"
global Type
Type = "None"
global FormLevel
FormLevel = 0
global Hardness
Hardness = 0
global Sharpness
Sharpness = 0
global Copper
Copper = 0
global Iron
Iron = 0
class simpleapp_tk(tkinter.Tk):
def __init__(self, parent):
tkinter.Tk.__init__(self, parent)
self.parent = parent
self.initialise()
def initialise(self):
self.grid()
self.resizable(False, False)
self.StartUp()
def StartUp(self):
#Story Section 1
self.labelVariableStory1 = tkinter.StringVar()
Story1 = tkinter.Label(self, textvariable=self.labelVariableStory1, anchor = 'w')
Story1.grid(column=0, row=0, sticky='EW')
self.labelVariableStory1.set("A great fire burned through the land two days ago.")
self.labelVariableStory2 = tkinter.StringVar()
Story2 = tkinter.Label(self, textvariable=self.labelVariableStory2, anchor = 'w')
Story2.grid(column=0, row=1, sticky='EW')
self.labelVariableStory2.set("It killed hundreds and left more homeless.")
self.labelVariableStory3 = tkinter.StringVar()
Story3 = tkinter.Label(self, textvariable=self.labelVariableStory3, anchor = 'w')
Story3.grid(column=0, row=2, sticky='EW')
self.labelVariableStory3.set("Now you walk the streets begging for money just to get by.")
self.labelVariableStory4 = tkinter.StringVar()
Story4 = tkinter.Label(self, textvariable=self.labelVariableStory4, anchor = 'w')
Story4.grid(column=0, row=3, sticky='EW')
self.labelVariableStory4.set("This is the start of your journey.")
Story1.after(15000, lambda: Story1.destroy())
Story2.after(15000, lambda: Story2.destroy())
Story3.after(15000, lambda: Story3.destroy())
Story4.after(15000, lambda: Story4.destroy())
self.after(15000, lambda: self.New_Day())
def New_Day(self):
global Coins
global Actions
global ActionSpeed
global Copper
global Iron
Actions = 5 + ActionSpeed
#Basic Information Labels
self.labelVariableInfo1 = tkinter.StringVar()
Info1 = tkinter.Label(self, textvariable=self.labelVariableInfo1, anchor = 'e')
Info1.grid(column=0, row=0, sticky='EW')
self.labelVariableInfo1.set("Coins:")
self.labelVariableInfo2 = tkinter.StringVar()
Info2 = tkinter.Label(self, textvariable=self.labelVariableInfo2, anchor = 'w')
Info2.grid(column=1, row=0, sticky='EW')
self.labelVariableInfo2.set(Coins)
self.labelVariableInfo3 = tkinter.StringVar()
Info3 = tkinter.Label(self, textvariable=self.labelVariableInfo3, anchor = 'e')
Info3.grid(column=2, row=0, sticky='EW')
self.labelVariableInfo3.set("Actions:")
self.labelVariableInfo4 = tkinter.StringVar()
Info4 = tkinter.Label(self, textvariable=self.labelVariableInfo4, anchor = 'w')
Info4.grid(column=3, row=0, sticky='EW')
self.labelVariableInfo4.set(Actions)
self.labelVariableInfo5 = tkinter.StringVar()
Info5 = tkinter.Label(self, textvariable=self.labelVariableInfo5, anchor = 'e')
Info5.grid(column=12, row=0, sticky='EW')
self.labelVariableInfo5.set("Day")
self.labelVariableInfo6 = tkinter.StringVar()
Info6 = tkinter.Label(self, textvariable=self.labelVariableInfo6, anchor = 'w')
Info6.grid(column=13, row=0, sticky='EW')
self.labelVariableInfo6.set(Day)
if Stage == 0:
LabelClear = tkinter.Label(self)
LabelClear.grid(column=4, row=1, columnspan=10, sticky='EW')
WorkButton1 = tkinter.Button(self, text=u"Beg", command=self.Beg)
WorkButton1.grid(column=0, row=1, columnspan=2, sticky='NESW')
SleepButton = tkinter.Button(self, text=u"Sleep", command=self.Sleep)
SleepButton.grid(column=2, row=1, columnspan=2, sticky='NESW')
if Stage >= 1:
#Border Section
self.labelVariableCutOff = tkinter.StringVar()
CutOff = tkinter.Label(self, textvariable=self.labelVariableCutOff)
CutOff.grid(column=0, row=1, columnspan=14, sticky='NESW')
self.labelVariableCutOff.set("+--------------------------------------------------------------+-------------------------------------+")
self.labelVariableBorder = tkinter.StringVar()
Border1 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border1.grid(column=0, row=2, sticky='W')
self.labelVariableBorder.set(" | ")
Border2 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border2.grid(column=13, row=2, sticky='E')
Border3 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border3.grid(column=0, row=3, sticky='W')
Border4 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border4.grid(column=13, row=3, sticky='E')
Border5 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border5.grid(column=0, row=4, sticky='W')
Border6 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border6.grid(column=13, row=4, sticky='E')
Border7 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border7.grid(column=0, row=5, sticky='W')
Border8 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border8.grid(column=13, row=5, sticky='E')
Border9 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border9.grid(column=0, row=6, sticky='W')
Border10 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border10.grid(column=13, row=6, sticky='E')
Border11 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border11.grid(column=0, row=7, sticky='W')
Border12 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border12.grid(column=13, row=7, sticky='E')
Border13 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border13.grid(column=0, row=8, sticky='W')
Border14 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border14.grid(column=13, row=8, sticky='E')
CutOff2 = tkinter.Label(self, textvariable=self.labelVariableCutOff)
CutOff2.grid(column=0, row=9, columnspan=14, sticky='NESW')
Border15 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border15.grid(column=0, row=10, sticky='W')
Border16 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border16.grid(column=13, row=10, sticky='E')
Border17 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border17.grid(column=0, row=11, sticky='W')
Border18 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border18.grid(column=13, row=11, sticky='E')
Border19 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border19.grid(column=0, row=12, sticky='W')
Border20 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border20.grid(column=13, row=12, sticky='E')
Border21 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border21.grid(column=0, row=13, sticky='W')
Border22 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border22.grid(column=13, row=13, sticky='E')
Border23 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border23.grid(column=0, row=14, sticky='W')
Border24 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border24.grid(column=13, row=14, sticky='E')
Border25 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border25.grid(column=7, row=2, sticky='E')
Border26 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border26.grid(column=7, row=3, sticky='E')
Border27 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border27.grid(column=7, row=4, sticky='E')
Border27 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border27.grid(column=7, row=5, sticky='E')
Border28 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border28.grid(column=7, row=6, sticky='E')
Border29 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border29.grid(column=7, row=7, sticky='E')
Border30 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border30.grid(column=7, row=8, sticky='E')
Border31 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border31.grid(column=7, row=10, sticky='E')
Border32 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border32.grid(column=7, row=11, sticky='E')
Border33 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border33.grid(column=7, row=12, sticky='E')
Border34 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border34.grid(column=7, row=13, sticky='E')
Border35 = tkinter.Label(self, textvariable=self.labelVariableBorder)
Border35.grid(column=7, row=14, sticky='E')
CutOff3 = tkinter.Label(self, textvariable=self.labelVariableCutOff)
CutOff3.grid(column=0, row=15, columnspan=14, sticky='NESW')
#Advanced Information Labels (Functional but not Fully Implemented)
self.labelVariableWork1 = tkinter.StringVar()
Work1 = tkinter.Label(self, textvariable=self.labelVariableWork1, anchor='e', fg='black')
Work1.grid(column=9, row=3, columnspan=3, sticky='EW')
self.labelVariableWork1.set("Metal:")
self.labelVariableWork2 = tkinter.StringVar()
Work2 = tkinter.Label(self, textvariable=self.labelVariableWork2, anchor='w', fg='black')
Work2.grid(column=12, row=3, sticky='EW')
self.labelVariableWork2.set(Metal)
self.labelVariableWork3 = tkinter.StringVar()
Work3 = tkinter.Label(self, textvariable=self.labelVariableWork3, anchor='e', fg='black')
Work3.grid(column=9, row=4, columnspan=3, sticky='EW')
self.labelVariableWork3.set("Type:")
self.labelVariableWork4 = tkinter.StringVar()
Work4 = tkinter.Label(self, textvariable=self.labelVariableWork4, anchor='w', fg='black')
Work4.grid(column=12, row=4, sticky='EW')
self.labelVariableWork4.set(Type)
self.labelVariableWork5 = tkinter.StringVar()
Work5 = tkinter.Label(self, textvariable=self.labelVariableWork5, anchor='e', fg='black')
Work5.grid(column=9, row=5, columnspan=3, sticky='EW')
self.labelVariableWork5.set("Forming:")
self.labelVariableWork6 = tkinter.StringVar()
Work6 = tkinter.Label(self, textvariable=self.labelVariableWork6, anchor='w', fg='black')
Work6.grid(column=12, row=5, sticky='EW')
self.labelVariableWork6.set("{0}%".format(FormLevel))
self.labelVariableWork7 = tkinter.StringVar()
Work7 = tkinter.Label(self, textvariable=self.labelVariableWork7, anchor='e', fg='black')
Work7.grid(column=9, row=6, columnspan=3, sticky='EW')
self.labelVariableWork7.set("Hardening:")
self.labelVariableWork8 = tkinter.StringVar()
Work8 = tkinter.Label(self, textvariable=self.labelVariableWork8, anchor='w', fg='black')
Work8.grid(column=12, row=6, sticky='EW')
self.labelVariableWork8.set("{0}%".format(Hardness))
self.labelVariableWork9 = tkinter.StringVar()
Work9 = tkinter.Label(self, textvariable=self.labelVariableWork9, anchor='e', fg='black')
Work9.grid(column=9, row=7, columnspan=3, sticky='EW')
self.labelVariableWork9.set("Sharpening:")
self.labelVariableWork10 = tkinter.StringVar()
Work10 = tkinter.Label(self, textvariable=self.labelVariableWork10, anchor='w', fg='black')
Work10.grid(column=12, row=7, sticky='EW')
self.labelVariableWork10.set("{0}%".format(Sharpness))
#Label that is meant to be next to the menu
Label1 = tkinter.Label(self, text="Buy:", anchor='e')
Label1.grid(column=1, row=2, sticky='EW')
#The Latest version of my unworking menu code
'''BuyMenu = tkinter.Menu()
Menu1 = tkinter.MenuButton(menu=BuyMenu)
BuyMenu.add_cascade(label="Choose Metal", menu=BuyMenu)
BuyMenu.add_command(label="Copper C: 15", command=self.BuyCopper)
BuyMenu.add_command(label="Iron C: 25", command=self.BuyIron)
Menu1.grid(column=2, row=2, sticky='EW')'''
#Resource Information
Info7 = tkinter.Label(self, text="Copper:", anchor='e')
Info7.grid(column=1, row=3, sticky='EW')
self.labelVariableResource1 = tkinter.StringVar()
Resource1 = tkinter.Label(self, textvariable=self.labelVariableResource1, anchor='w')
Resource1.grid(column=2, row=3, sticky='EW')
self.labelVariableResource1.set(Copper)
Info8 = tkinter.Label(self, text="Iron:", anchor='e')
Info8.grid(column=1, row=4, sticky='EW')
self.labelVariableResource2 = tkinter.StringVar()
Resource1 = tkinter.Label(self, textvariable=self.labelVariableResource2, anchor='w')
Resource1.grid(column=2, row=4, sticky='EW')
self.labelVariableResource2.set(Iron)
#Stage 0 Commands
def Beg(self):
global Actions
global Coins
if Actions != 0:
Actions -= 1
Coins += 1
Bonus = randrange(1,6)
if Bonus == 5:
Coins +=1
self.labelVariableInfo2.set(Coins)
self.labelVariableInfo4.set(Actions)
def Sleep(self):
global Day
global Coins
Day += 1
if Stage == 0:
Coins -= 2
DarkLabel = tkinter.Label(self, bg='black')
DarkLabel.grid(column=0, row=0, columnspan=14, rowspan=2, sticky='NESW')
self.after(5000, lambda: DarkLabel.destroy())
if Day == 5:
self.after(5000, lambda: self.Story1())
Coins += 50
else:
self.after(5000, lambda: self.New_Day())
#Story Section 2
def Story1(self):
global Stage
self.labelVariableInfo5.set("")
self.labelVariableInfo6.set("")
self.labelVariableStory1 = tkinter.StringVar()
Story1 = tkinter.Label(self, textvariable=self.labelVariableStory1, anchor = 'w')
Story1.grid(column=0, row=0, columnspan=4, sticky='EW')
self.labelVariableStory1.set("On the fifth day an old man comes up to you.")
self.labelVariableStory2 = tkinter.StringVar()
Story2 = tkinter.Label(self, textvariable=self.labelVariableStory2, anchor = 'w')
Story2.grid(column=0, row=1, columnspan=4, sticky='NESW')
self.labelVariableStory2.set("'It pains me to see a young person in need.'")
self.labelVariableStory3 = tkinter.StringVar()
Story3 = tkinter.Label(self, textvariable=self.labelVariableStory3, anchor = 'w')
Story3.grid(column=0, row=2, columnspan=4, sticky='EW')
self.labelVariableStory3.set("'I'm almost at the end of my days and I have no children.'")
self.labelVariableStory4 = tkinter.StringVar()
Story4 = tkinter.Label(self, textvariable=self.labelVariableStory4, anchor = 'w')
Story4.grid(column=0, row=3, columnspan=4, sticky='EW')
self.labelVariableStory4.set("'I will teach you all I can and leave you my forge.'")
Story1.after(15000, lambda: Story1.destroy())
Story2.after(15000, lambda: Story2.destroy())
Story3.after(15000, lambda: Story3.destroy())
Story4.after(15000, lambda: Story4.destroy())
Stage += 1
self.after(15000, lambda: self.New_Day())
#Stage 1 Commands (These are to be triggered by the menu)
def BuyCopper(self):
global Coins
global Copper
if Coins >= 15:
Coins -= 15
Copper += 1
self.labelVariableInfo2.set(Coins)
self.labelVariableResource1.set(Copper)
def BuyIron(self):
global Coins
global Iron
if Coins >= 25:
Coins -= 25
Iron += 1
self.labelVariableInfo2.set(Coins)
self.labelVariableResource2.set(Iron)
#GUI loop
if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('Smithy')
app.mainloop()
我现在已经包含了我的整个代码,但注释掉了我的工作菜单代码。
答案 0 :(得分:0)
您需要option menu或ComboBox。它们是菜单,但在页面上不在屏幕顶部。
以下是向OptionMenu添加选项的代码:
from Tkinter import *
root=Tk()
def change(value):
options.append("YOUR_OPTION_HERE")
menu=drop["menu"]
menu.delete("0", END)
for string in options:
menu.add_command(label=string, command=lambda value=string: var.set(value))
options=["hello", "goodbye", "welcome"]
var=StringVar()
drop=OptionMenu(root, var, *options, command=change)
drop.config(width=15)
drop.pack()
root.mainloop()