Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使在循环中创建的Python Tkinter小部件独立?_Python_For Loop_User Interface_Tkinter_Listbox - Fatal编程技术网

如何使在循环中创建的Python Tkinter小部件独立?

如何使在循环中创建的Python Tkinter小部件独立?,python,for-loop,user-interface,tkinter,listbox,Python,For Loop,User Interface,Tkinter,Listbox,我已经尝试过通过追加将小部件放入列表,但运气不佳。我在for循环中创建了所有内容,但我希望每个月的列表框都有自己的滚动条。目前只有最后一个列表框工作正常,移动其他月份滚动条会移动12月份的列表框 最终,每天都可以点击,并打开另一个窗口添加每日交易 由于将重复创建同一组小部件,因此最好将它们嵌入到类中: import tkinter as tk class BalanceLog(tk.Frame): months = [ ['January', 31],

我已经尝试过通过追加将小部件放入列表,但运气不佳。我在for循环中创建了所有内容,但我希望每个月的列表框都有自己的滚动条。目前只有最后一个列表框工作正常,移动其他月份滚动条会移动12月份的列表框

最终,每天都可以点击,并打开另一个窗口添加每日交易


由于将重复创建同一组小部件,因此最好将它们嵌入到类中:

import tkinter as tk

class BalanceLog(tk.Frame):
    months = [
        ['January', 31],
        ['February', 28],  # Check for leap year
        ['March', 31],
        ['April', 30],
        ['May', 31],
        ['June', 30],
        ['July', 31],
        ['August', 31],
        ['September', 30],
        ['October', 31],
        ['November', 30],
        ['December', 31]
    ]
    def __init__(self, parent, month):
        super().__init__(parent)

        tk.Label(self, text=self.months[month-1][0], font='CourierNew 12 bold').grid(row=0, column=0, columnspan=3)
        tk.Label(self, text='Day', bd=1, relief='raised').grid(row=1, column=0, sticky='ew')
        tk.Label(self, text='Ending Balance', bd=1, relief='raised', anchor='e').grid(row=1, column=1, sticky='ew')

        sb = tk.Scrollbar(self, orient='vertical', command=self.on_scroll)
        sb.grid(row=2, column=2, sticky='ns')

        self.day_list = tk.Listbox(self, width=3, activestyle='none', exportselection=0, yscrollcommand=sb.set)
        self.day_list.grid(row=2, column=0, sticky='ew')
        self.day_list.bind('<<ListboxSelect>>', self.on_select)

        self.balance_list = tk.Listbox(self, width=16, activestyle='none', exportselection=0, justify='right', yscrollcommand=sb.set)
        self.balance_list.grid(row=2, column=1, sticky='ew')
        self.balance_list.bind('<<ListboxSelect>>', self.on_select)

        # fill the listboxes
        for day in range(1, self.months[month-1][1]+1):
            self.day_list.insert('end', day)
            self.balance_list.insert('end', str(day)+',999,999.99')

    def on_scroll(self, *args):
        self.day_list.yview(*args)
        self.balance_list.yview(*args)

    def on_select(self, event):
        selected = event.widget.curselection()[0]
        # update selection of another listbox
        another = self.balance_list if event.widget is self.day_list else self.day_list
        another.selection_clear(0, 'end')
        another.selection_set(selected)
        another.see(selected)

由于将重复创建同一组小部件,因此最好将它们嵌入到类中:

import tkinter as tk

class BalanceLog(tk.Frame):
    months = [
        ['January', 31],
        ['February', 28],  # Check for leap year
        ['March', 31],
        ['April', 30],
        ['May', 31],
        ['June', 30],
        ['July', 31],
        ['August', 31],
        ['September', 30],
        ['October', 31],
        ['November', 30],
        ['December', 31]
    ]
    def __init__(self, parent, month):
        super().__init__(parent)

        tk.Label(self, text=self.months[month-1][0], font='CourierNew 12 bold').grid(row=0, column=0, columnspan=3)
        tk.Label(self, text='Day', bd=1, relief='raised').grid(row=1, column=0, sticky='ew')
        tk.Label(self, text='Ending Balance', bd=1, relief='raised', anchor='e').grid(row=1, column=1, sticky='ew')

        sb = tk.Scrollbar(self, orient='vertical', command=self.on_scroll)
        sb.grid(row=2, column=2, sticky='ns')

        self.day_list = tk.Listbox(self, width=3, activestyle='none', exportselection=0, yscrollcommand=sb.set)
        self.day_list.grid(row=2, column=0, sticky='ew')
        self.day_list.bind('<<ListboxSelect>>', self.on_select)

        self.balance_list = tk.Listbox(self, width=16, activestyle='none', exportselection=0, justify='right', yscrollcommand=sb.set)
        self.balance_list.grid(row=2, column=1, sticky='ew')
        self.balance_list.bind('<<ListboxSelect>>', self.on_select)

        # fill the listboxes
        for day in range(1, self.months[month-1][1]+1):
            self.day_list.insert('end', day)
            self.balance_list.insert('end', str(day)+',999,999.99')

    def on_scroll(self, *args):
        self.day_list.yview(*args)
        self.balance_list.yview(*args)

    def on_select(self, event):
        selected = event.widget.curselection()[0]
        # update selection of another listbox
        another = self.balance_list if event.widget is self.day_list else self.day_list
        another.selection_clear(0, 'end')
        another.selection_set(selected)
        another.see(selected)

关于在循环中创建小部件,stackoverflow有很多问题。你有没有通读过其中任何一个来理解基本原理?我已经读过很多这样的问题,但是我很困惑,一旦小部件存储在列表中,我应该做什么。我想它会像dayLabelID[I].config,dayLabelID[I].grid那样工作。问问自己:如果
滚动两个
调用
self.daysList.yview
,你认为它应该如何知道应该关注循环的哪个迭代
self.daysList
在任何时候都只能引用一个小部件。我得到了它,并为它们添加了索引,但没有骰子。我尝试将command=functools.partial(self.scrollThread,idx=I)添加到滚动条并将idx传递给scrollThread,但随后出现了一个错误:“scrollThread()为参数'idx'获取了多个值”。我用最近尝试的方法更新了上面的代码。关于在循环中创建小部件的stackoverflow,有许多问题。你有没有通读过其中任何一个来理解基本原理?我已经读过很多这样的问题,但是我很困惑,一旦小部件存储在列表中,我应该做什么。我想它会像dayLabelID[I].config,dayLabelID[I].grid那样工作。问问自己:如果
滚动两个
调用
self.daysList.yview
,你认为它应该如何知道应该关注循环的哪个迭代
self.daysList
在任何时候都只能引用一个小部件。我得到了它,并为它们添加了索引,但没有骰子。我尝试将command=functools.partial(self.scrolldeath,idx=I)添加到滚动条并将idx传递给scrolldeath,但随后出现了一个错误:“scrolldeath()为参数'idx'获取了多个值”。我用最近尝试的内容更新了上述代码。感谢您的帮助,我只是想大体上理解Tkitner,因为当涉及到除了简单的小部件之外的任何事情时,文档都非常简单。我使用列表更新了上面的代码,但无法同时滚动两个列表框。您是否使用了我的代码,因为它在我的Python环境中运行良好。还没有,因为我不完全理解嵌入,也不想编写我不理解的代码。我想尝试两种方法,一种是使用您的嵌入方法,另一种是使用列表更新我的代码。感谢您的帮助,我只是想大致了解Tkitner,因为当涉及到除简单小部件之外的任何操作时,文档都非常简单。我使用列表更新了上面的代码,但无法同时滚动两个列表框。您是否使用了我的代码,因为它在我的Python环境中运行良好。还没有,因为我不完全理解嵌入,也不想编写我不理解的代码。我想尝试两种方法,一种是使用嵌入方法,另一种是使用列表更新代码。
root = tk.Tk()
for month in range(1, 13):
    BalanceLog(root, month).pack(side='left')
root.mainloop()