我从列表中获取元素时遇到问题。当我按下键盘的Page down按钮时,它将调用PageDown函数,因为它将跳过7个元素,它将从列表中获取7个下一个元素:
['108 Sky Atlantic', '401 Sky Sports 1 UK', '402 SKY Sports 2 UK', '403 Sky Sports 3 UK', '404 Sky Sports 4 UK', '405 Sky Sports 5 UK', '406 Sky Sports News HQ'
]
当我点击键盘的向下箭头按钮时,它将跳过7个元素,但它不会获取406 Sky Sports News HQ
之后的7个元素,因为它将获取它将在413 Eurosport 1 UK
之后出现的元素{1}}所以channels_list
会显示不正确的内容:
['414 Eurosport 2 UK', '415 BT Sport 1', '416 BT Sport 2',
'417 At the Races', '418 BT Sport ESPN', '419 MUTV', '420 Chelsea TV']
应该是:
['407 Sky Sports F1', '408 Sky Sports 1 HD', '409 Sky Sports 2 HD', '410 Sky Sports 3 HD', '411 Sky Sports 4 HD', '412 Sky Sports 5 HD', '413 Eurosport 1 UK']
以下是代码:
def PageDown(self):
self.channel = [
'101 BBC One S East', '102 BBC Two', '103 ITV', '104 Channel 4',
'105 Channel 5', '106 Sky One', '107 Sky Living', '108 Sky Atlantic',
'401 Sky Sports 1 UK', '402 SKY Sports 2 UK', '403 Sky Sports 3 UK',
'404 Sky Sports 4 UK', '405 Sky Sports 5 UK', '406 Sky Sports News HQ',
'407 Sky Sports F1', '408 Sky Sports 1 HD', '409 Sky Sports 2 HD',
'410 Sky Sports 3 HD', '411 Sky Sports 4 HD', '412 Sky Sports 5 HD',
'413 Eurosport 1 UK', '414 Eurosport 2 UK', '415 BT Sport 1',
'416 BT Sport 2', '417 At the Races', '418 BT Sport ESPN', '419 MUTV',
'420 Chelsea TV', '421 Eir Sport 1', '422 Eir Sport 2']
channels_list = list()
channels_Index += 7
for i, start_ch in enumerate(self.channel):
if start_ch == channel:
channels_list =
self.channel[i+self.channels_Index:i+self.channels_Index+7]
我期望实现的是当我点击键盘的向下翻页按钮时,我想跳过7个元素并每次获取7个元素。
这是例如:
当我点击页面向下按钮时,我想跳过7个元素并获取显示如下所示的7个下一个元素: ['108 Sky Atlantic','401 Sky Sports 1 UK','402 SKY Sports 2 UK','403 Sky Sports 3 UK','404 Sky Sports 4 UK','405 Sky Sports 5 UK','406 Sky体育新闻总部']
所以当我点击页面向下按钮时,我想获得7个下一个元素:
['407 Sky Sports F1', '408 Sky Sports 1 HD', '409 Sky Sports 2 HD', '410 Sky Sports 3 HD', '411 Sky Sports 4 HD', '412 Sky Sports 5 HD', '413 Eurosport 1 UK']
当我再次点击页面向下按钮时,我想获取7个下一个元素:
['414 Eurosport 2 UK', '415 BT Sport 1', '416 BT Sport 2', '417 At the Races', '418 BT Sport ESPN', '419 MUTV', '420 Chelsea TV']
当我最后再次点击页面向下按钮时,我想获取接近列表末尾的7个元素,使其显示如下:
['416 BT Sport 2', '417 At the Races', '418 BT Sport ESPN', '419 MUTV', '420 Chelsea TV', '421 Eir Sport 1', '422 Eir Sport 2']
你能否告诉我一个例子,当我点击键盘的页面向下按钮时,我每次跳过7个元素并从列表中取出7个元素?
答案 0 :(得分:1)
移动线
channels_Index += 7
现在应该可以了。将它放在循环之后,以便可以再次调用它。
答案 1 :(得分:1)
这演示了一次滚动浏览7个频道,关键部分是next_seven_channels = self.all_channels[self.channel_index:end_index]
try:
import Tkinter as tk
except:
import tkinter as tk
class app(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.all_channels = [
'101 BBC One S East', '102 BBC Two', '103 ITV', '104 Channel 4',
'105 Channel 5', '106 Sky One', '107 Sky Living', '108 Sky Atlantic',
'401 Sky Sports 1 UK', '402 SKY Sports 2 UK', '403 Sky Sports 3 UK',
'404 Sky Sports 4 UK', '405 Sky Sports 5 UK', '406 Sky Sports News HQ',
'407 Sky Sports F1', '408 Sky Sports 1 HD', '409 Sky Sports 2 HD',
'410 Sky Sports 3 HD', '411 Sky Sports 4 HD', '412 Sky Sports 5 HD',
'413 Eurosport 1 UK', '414 Eurosport 2 UK', '415 BT Sport 1',
'416 BT Sport 2', '417 At the Races', '418 BT Sport ESPN', '419 MUTV',
'420 Chelsea TV', '421 Eir Sport 1', '422 Eir Sport 2']
self.channel_index = 0
self.print_channels = tk.Button(self,text="Page Down",
font=("Calibri",30,"bold"),
foreground="DodgerBlue2",
command=self.PageDown)
self.print_channels.pack()
def PageDown(self,event=None):
end_index = self.channel_index + 7
if end_index > len(self.all_channels):
end_index -= len(self.all_channels) - end_index
next_seven_channels = self.all_channels[self.channel_index:end_index]
self.channel_index += 7
if self.channel_index > len(self.all_channels):
self.channel_index = 0
print (next_seven_channels)
root = app()
root.mainloop()
如果我理解正确,要立即显示前7个频道,请在节目开始后拨打self.PageDown()
,如下所示:
try:
import Tkinter as tk
except:
import tkinter as tk
class app(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.all_channels = [
'101 BBC One S East', '102 BBC Two', '103 ITV', '104 Channel 4',
'105 Channel 5', '106 Sky One', '107 Sky Living', '108 Sky Atlantic',
'401 Sky Sports 1 UK', '402 SKY Sports 2 UK', '403 Sky Sports 3 UK',
'404 Sky Sports 4 UK', '405 Sky Sports 5 UK', '406 Sky Sports News HQ',
'407 Sky Sports F1', '408 Sky Sports 1 HD', '409 Sky Sports 2 HD',
'410 Sky Sports 3 HD', '411 Sky Sports 4 HD', '412 Sky Sports 5 HD',
'413 Eurosport 1 UK', '414 Eurosport 2 UK', '415 BT Sport 1',
'416 BT Sport 2', '417 At the Races', '418 BT Sport ESPN', '419 MUTV',
'420 Chelsea TV', '421 Eir Sport 1', '422 Eir Sport 2']
self.channel_index = 0
self.print_channels = tk.Button(self,text="Page Down",
font=("Calibri",30,"bold"),
foreground="DodgerBlue2",
command=self.PageDown)
self.print_channels.pack()
self.PageDown() # added code <----
def PageDown(self,event=None):
end_index = self.channel_index + 7
if end_index > len(self.all_channels):
end_index -= len(self.all_channels) - end_index
next_seven_channels = self.all_channels[self.channel_index:end_index]
self.channel_index += 7
if self.channel_index > len(self.all_channels):
self.channel_index = 0
print (next_seven_channels)
root = app()
root.mainloop()
或者在程序启动时跳过显示前7个频道:
try:
import Tkinter as tk
except:
import tkinter as tk
class app(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.all_channels = [
'101 BBC One S East', '102 BBC Two', '103 ITV', '104 Channel 4',
'105 Channel 5', '106 Sky One', '107 Sky Living', '108 Sky Atlantic',
'401 Sky Sports 1 UK', '402 SKY Sports 2 UK', '403 Sky Sports 3 UK',
'404 Sky Sports 4 UK', '405 Sky Sports 5 UK', '406 Sky Sports News HQ',
'407 Sky Sports F1', '408 Sky Sports 1 HD', '409 Sky Sports 2 HD',
'410 Sky Sports 3 HD', '411 Sky Sports 4 HD', '412 Sky Sports 5 HD',
'413 Eurosport 1 UK', '414 Eurosport 2 UK', '415 BT Sport 1',
'416 BT Sport 2', '417 At the Races', '418 BT Sport ESPN', '419 MUTV',
'420 Chelsea TV', '421 Eir Sport 1', '422 Eir Sport 2']
self.channel_index = 7 # changed code <----
self.print_channels = tk.Button(self,text="Page Down",
font=("Calibri",30,"bold"),
foreground="DodgerBlue2",
command=self.PageDown)
self.print_channels.pack()
def PageDown(self,event=None):
end_index = self.channel_index + 7
if end_index > len(self.all_channels):
end_index -= len(self.all_channels) - end_index
next_seven_channels = self.all_channels[self.channel_index:end_index]
self.channel_index += 7
if self.channel_index > len(self.all_channels):
self.channel_index = 0
print (next_seven_channels)
root = app()
root.mainloop()
答案 2 :(得分:1)
您更改了channels_Index
的值,这是一个您尚未在代码中发布的全局变量(我假设;否则python将抛出NameError),但您读取的值{{1 }}。你可能意味着self.channels_Index