我正在尝试为我的高中班级创建一个示例程序。该程序的想法是您可以从单选按钮中选择一个游戏(Steam的基本版本)。单选按钮链接到存储在self.library_game中的IntVar。当您选择单选按钮时,我要使用单选按钮分配给Intvar的值,以从列表中选取一个名称,并在标签中显示游戏名称。我想这样做,所以我可以使用IntVar访问其他列表。其他列表将显示诸如游戏安装状态之类的信息。如果当前未“安装”游戏,则可以单击按钮以将状态更新为“已安装”游戏,以便可以“启动”游戏。
我遇到的问题是:当我尝试使用IntVar访问libraryGames列表时,出现以下错误:“列表索引必须是整数或切片,而不是方法”。我尝试使用“ test = self.library_game.get”将IntVar中的值存储为常规整数,并使用“ test”访问列表self.game_selection_label.configure(text =“您已选择:” + str (libraryGames [test]),fg =“#01fc07”),但它不起作用。
我不是最强大的程序员,并且确实在用这些东西来提高自己的极限,所以任何帮助都将是惊人的。
from tkinter import*
# Variables and Lists
#Classes
libraryGames=["The Witcher 3: Wild Hunt GOTE", "Jurassic World: Evolution", "Red Dead Redemption 2","Mass Effect Trilogy","Subnautica"]
libraryGamesInstall=[True,False, False, False, False]
class SteamGUI:
def __init__(self, parent):
#variables
global libraryGames
global libraryGamesInstall
WD=800
self.header =PhotoImage(file = "header.gif")
self.library_game = IntVar()
self.library_game.set = ()
Title=Label(parent, image = self.header, width=WD, anchor=N)
Title.grid(row=0, column=0,columnspan=2, sticky=N,padx=2, pady=2)
#User Library Menu
frame1=Frame(bg="#000000",)
frame1.grid(row=1, column=0, sticky = 'w')
library_label=Label(frame1, text="User Library",bg="#000000",fg="#01fc07",font=("Eras Demi ITC","40"), anchor=N)
library_label.grid(row=0, column=0,columnspan=2, sticky=N,padx=2, pady=2)
radio1 = Radiobutton(frame1, variable = self.library_game, value = 0,text =libraryGames[0],
bg="#000000",fg="#ffffff",font=("Calibri","20"), command = self.library_choice)
radio1.grid(row=1, column=0, columnspan=2, sticky = 'w')
radio2 = Radiobutton(frame1, variable = self.library_game, value =1,text =libraryGames[1],
bg="#000000",fg="#ffffff",font=("Calibri","20"), command = self.library_choice)
radio2.grid(row=2, column=0, columnspan=2, sticky = 'w')
radio3 = Radiobutton(frame1, variable = self.library_game, value =2,text =libraryGames[2],
bg="#000000",fg="#ffffff",font=("Calibri","20"),command = self.library_choice)
radio3.grid(row=3, column=0, columnspan=2, sticky = 'w')
radio4 = Radiobutton(frame1, variable = self.library_game, value =3,text =libraryGames[3],
bg="#000000",fg="#ffffff",font=("Calibri","20"),command = self.library_choice)
radio4.grid(row=4, column=0, columnspan=2, sticky = 'w')
radio5 = Radiobutton(frame1, variable = self.library_game, value =4,text =libraryGames[4],
bg="#000000",fg="#ffffff",font=("Calibri","20"),command = self.library_choice)
radio5.grid(row=5, column=0, columnspan=2, sticky = 'w')
self.game_selection_label=Label(frame1, text="No game selected", bg="#000000", fg="#ffffff", width="50",)
self.game_selection_label.grid(row=6, column=0, columnspan=2, sticky='w')
self.game_install_status_label=Label(frame1, text="Install Status", bg="#000000", fg="#ffffff", width="50",)
self.game_install_status_label.grid(row=7, column=0, sticky='w', pady=10)
self.playLabel =Label(frame1,text="Game Status", bg="#000000", fg="#ffffff", width="50", pady=10)
self.playLabel.grid(row=9, column=0, sticky='w')
installButton=Button(frame1, text="Install", width = 20, font=("Eras Demi ITC","10"), pady=10)
installButton.grid(row=8, column=0, sticky = 'sw',)
playButton=Button(frame1, text="Play", width = 20,font=("Eras Demi ITC","10"), pady=10)
playButton.grid(row=8, column=0, sticky = 'se',)
frame2=Frame(bg="#ffffff",)
frame2.grid(row=1, column=1)
def library_choice(self):
test=0
test=self.library_game.get
print("hi", self.library_game.get)
self.game_selection_label.configure(text = "You have chosen: " + str(libraryGames[test]),fg="#01fc07")
#self.game_selection_label.configure(text = "You have chosen: The witcher 3!!!!!",fg="#01fc07")
#Main Routine
root=Tk()
window = SteamGUI(root)
root.geometry("800x700+0+0")
root.title("Steam Basic")
root.mainloop()