好的,对于这个GUI,我想根据某个变量的结果来读取一个文件。我会尽量详细解释的
所以这个文件从下拉菜单中获取输入。用户选择的选项将决定读取哪个文件。然后将读取的文件追加到数组中,然后将数组放入另一个下拉菜单中
我想知道这是否可行。我的部分代码在下面
早期下拉菜单:
weapon.set('Sword')
choices = ['Sword', 'Bow', 'Katana']
self.weaponMenu = OptionMenu(self,weapon, *choices)
self.weaponMenu.grid(row =2, column = 2)
读取文件:
self.moves = Label(self, text = "Move")
self.moves.grid(row = 9, column = 1,)
move = StringVar()
weapon = open("%d.txt" %(self.weaponMenu.get()),"r") #reads weapon file
moves = []
for line in weapon:
moves.append(line)
self.movesMenu = OptionMenu(self, move, *moves)
self.movesMenu.grid(row =10, column = 1)
这是我试图做的,但我无法让它工作。我可以用if语句代替,但是我想知道我尝试的方法是否可行,或者是否有其他方法可以达到相同的结果
希望您能理解我写的内容,并提前感谢您的帮助!希望我做的一切都对,我还是新手
当我尝试执行该文件时,会收到以下错误消息:
我收到以下错误消息:
Traceback (most recent call last):
File "C:\Python27\mh4uDamageWindow.py", line 88, in <module>
app = Application(root)
File "C:\Python27\mh4uDamageWindow.py", line 10, in __init__
self.create_widget()
File "C:\Python27\mh4uDamageWindow.py", line 67, in create_widget
weapon = open("%d.txt" % (self.weaponMenu.get()),"r") #reads weapon file
AttributeError: OptionMenu instance has no attribute 'get'
>>>
使用为
OptionMenu
(weapon
)而不是小部件本身设置的变量:如果在读取文件的作用域中不可用,请将其添加到
self
相关问题 更多 >
编程相关推荐