Python 3.x 如何在python中进行动态列表更改?
Guy正在尝试为我的tk GUI创建页面,所以我在下面得到了这段代码,它工作得非常好,单击next按钮,它会得到前5个页面,直到100年底,反之亦然,单击prev按钮Python 3.x 如何在python中进行动态列表更改?,python-3.x,tkinter,Python 3.x,Tkinter,Guy正在尝试为我的tk GUI创建页面,所以我在下面得到了这段代码,它工作得非常好,单击next按钮,它会得到前5个页面,直到100年底,反之亦然,单击prev按钮 import tkinter as tk import math items = [str(n) for n in range(100)] page = 0 per_page = 5 n_pages = math.ceil(len(items) / per_page) def update_list(): prin
import tkinter as tk
import math
items = [str(n) for n in range(100)]
page = 0
per_page = 5
n_pages = math.ceil(len(items) / per_page)
def update_list():
print(page)
start_index = int(page * per_page)
end_index = int((page + 1) * per_page)
items_in_page = items[start_index:end_index]
view_text = "Page %d/%d: %s" % (page + 1, n_pages, ", ".join(items_in_page))
def change_page(delta):
global page
page = min(n_pages - 1, max(0, page + delta))
update_list()
def prev_btn():
change_page(-1)
def next_btn():
change_page(+1)
win = tk.Tk()
win.title("clickers")
tk.Button(win, text="next", command=next_btn).pack()
tk.Button(win, text="prev", command=prev_btn).pack()
show = tk.Entry(win)
show.pack()
update_list() # to initialize `show`
win.mainloop()
我想调整这部分代码
items = [str(n) for n in range(100)]
进入这样一个列表,并让它工作,只是切换它们不起作用
list_ = [1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15], [16,17,18]]
items =[str(n) for n in list_]
因此,单击next按钮,它将返回前2行(任意数字选择)列表,反之亦然。完成后,请注意,此列表最初来自已选择的数据库的多行,而不仅仅是像我这样保存列表的某个变量,因此我需要关于如何直接使用循环获取前2行(2个列表)的帮助从数据库,并立即过去2行为prev按钮
import tkinter as tk
import math
items = [str(n) for n in range(100)]
page = 0
per_page = 5
n_pages = math.ceil(len(items) / per_page)
def update_list():
print(page)
start_index = int(page * per_page)
end_index = int((page + 1) * per_page)
items_in_page = items[start_index:end_index]
view_text = "Page %d/%d: %s" % (page + 1, n_pages, ", ".join(items_in_page))
def change_page(delta):
global page
page = min(n_pages - 1, max(0, page + delta))
update_list()
def prev_btn():
change_page(-1)
def next_btn():
change_page(+1)
win = tk.Tk()
win.title("clickers")
tk.Button(win, text="next", command=next_btn).pack()
tk.Button(win, text="prev", command=prev_btn).pack()
show = tk.Entry(win)
show.pack()
update_list() # to initialize `show`
win.mainloop()