在for循环中创建不同的复选框,可以单独引用
我正在创建一个节目跟踪器,在实际的跟踪器页面上,我想为每个检索的剧集加载带有一个复选框的卷轴卷轴框(我选择了一个卷轴盒,因为它允许我滚动,如果剧集填充了,则可以滚动屏幕)。下面的代码完美地完成了此操作,但是每个复选框都称为相同的变量,因此,每次我单击一个情节编号时,都会选择所有复选框。
for alignment in range(1,int(showDisplay_episodesAmount)+1):
# create a radio button
self.showDisplay.episodeCheckbox = Checkbutton(self.showDisplay.episodesScrollBox, text=alignment,variable=var1, onvalue=1, offvalue=0)
self.showDisplay.episodeCheckbox.grid(column=0, row=grid_column)
self.showDisplay.episodesScrollBox.window_create('end', window=self.showDisplay.episodeCheckbox)
self.showDisplay.episodesScrollBox.insert('end', '\n')
grid_column += 1
我希望每个复选框都与一个单独的剧集编号相对应(例如,涉及第1集的复选框1等),因为我无法确定各种节目的情节数量。我尝试了列表方法,制作此代码,两者都不会创建单独的复选框,并禁用滚动条:
for alignment in range(1,int(showDisplay_episodesAmount)+1):
# create a radio button
self.showDisplay.episodeCheckbox[alignment] = {}
self.showDisplay.episodeCheckbox[alignment]['name'] = alignment
self.showDisplay.episodeCheckbox[alignment]['checkbox'] = Checkbutton(self.showDisplay.episodesScrollBox, text=alignment,variable=var1, onvalue=1, offvalue=0).grid(column=0, row=grid_column)
#self.showDisplay.episodesScrollBox.window_create('end', window=self.showDisplay.episodeCheckbox[alignment])
self.showDisplay.episodesScrollBox.insert('end', '\n')
grid_column += 1
如何使每个复选框生成不同而不是彼此链接?
I'm creating a show tracker, and on the actual tracker page I want to load a scrolling ScrolledText box with a checkbox for each episode retrieved (I have chosen a ScrolledText box as it allows me to scroll if the amount of episodes fills over the screen). The code below does this perfectly, however each checkbox is referred to as the same variable, and thus every time I click one episode number, it selects all of them.
for alignment in range(1,int(showDisplay_episodesAmount)+1):
# create a radio button
self.showDisplay.episodeCheckbox = Checkbutton(self.showDisplay.episodesScrollBox, text=alignment,variable=var1, onvalue=1, offvalue=0)
self.showDisplay.episodeCheckbox.grid(column=0, row=grid_column)
self.showDisplay.episodesScrollBox.window_create('end', window=self.showDisplay.episodeCheckbox)
self.showDisplay.episodesScrollBox.insert('end', '\n')
grid_column += 1
I would like each checkbox to correspond to a separate episode number (such as checkbox 1 referring to episode 1, etc.), created in a for loop, as I cannot predetermine the amount of episodes for a wide variety of shows.I have tried the list approach, making this code, which both doesn't create separate checkboxes, and disables the scrollbar:
for alignment in range(1,int(showDisplay_episodesAmount)+1):
# create a radio button
self.showDisplay.episodeCheckbox[alignment] = {}
self.showDisplay.episodeCheckbox[alignment]['name'] = alignment
self.showDisplay.episodeCheckbox[alignment]['checkbox'] = Checkbutton(self.showDisplay.episodesScrollBox, text=alignment,variable=var1, onvalue=1, offvalue=0).grid(column=0, row=grid_column)
#self.showDisplay.episodesScrollBox.window_create('end', window=self.showDisplay.episodeCheckbox[alignment])
self.showDisplay.episodesScrollBox.insert('end', '\n')
grid_column += 1
How could I make each checkbox generated distinct and not linked to each other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用简单的Jhonatan版本,但是您也可以做一些更复杂的事情,例如创建一个词典,其中键是情节的渐进式计数器(您的对齐方式),而值是复选框本身,您可以从中访问状态。
我不使用普通的TK复选框,而是TTK,所以这是TTK的一个示例:
在这里,我假设您有兴趣仅显示复选框的状态,因此我省略了VAR。
您可以使用此行访问复选框的状态:
当然,MY_EPISODE是0和ShowDisplay_episodesamount之间的整数。您也可以扫描本词典以打印所有情节的状态(已检查 /未选中)。
最后注意,在ttk中,状态()方法的返回不是二进制的,而是一个具有许多值的字符串(“选择”,“焦点”和许多其他值,并且其中两个或多个可以共存,以便输出将是这些字符串的列表。 a>
我没有检查我的代码,所以请查看
You can use the simple version of Jhonatan but you can also do something more sophisticated like creating a dictionary in which the key is the progressive counter of episode (your alignment) and the value is the checkboxed itself, from which you can access the state.
I don't use plain tk checkboxes but rather ttk, so here it's an example with ttk:
Here I'm assuming you are interested in only showing the state of the checkboxes so I omitted the var.
You can access the state of your checkboxes with this line:
Where of course my_episode is an integer between 0 and showDisplay_episodesAmount. You can also sweep this dictionary to print the status of all the episodes (checked / unchecked).
Final note, in ttk the return of the state() method is not binary, rather is a string that has many values ("selected", "focus" and many others, and two or more of them can coexist so the output will be a list of these string. Look at the documentation
I haven't check my code so please review it
我认为您可以使用列表而不是变量。
然后,您可以将每个情节设置为列表中的一个项目。像这样:
现在每个复选框都会在列表中定义一个项目...
希望我有帮助。
乔纳森
I think you can use a list instead of the variable.
Then you can set each episode to one item in the list. like this:
Now each checkbox will define one item in the list ...
Hope I helped.
Jonathan