【发布时间】:2020-12-26 10:19:55
【问题描述】:
我有三个 MultiListbox 项目和两个条目。当我双击 MultiListbox 项目时,MultiListbox 选定的行信息将出现在条目中。出于这个原因,我试图在我的 MultiListbox 类中添加一个函数并在另一个类中调用这个函数,但我不知道该怎么做。 这是我的代码:
from tkinter import *
from tkinter.ttk import *
import tkinter as tk
import sqlite3
from tkinter import messagebox
class MultiListbox(Frame):
def __init__(self, master, lists):
Frame.__init__(self, master)
self.grid(sticky="nsew")
self.select_index = None
i = 2
for num, (l, w) in enumerate(lists):
if num == 0:
continue
self.grid_columnconfigure(num, weight=w, uniform='fred')
i += num
self.grid_rowconfigure(1, weight=1)
self.lists = []
for num, (l, w) in enumerate(lists):
frame = Frame(self, borderwidth=0)
frame.grid_columnconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid(row=1, column=num, sticky="nsew")
lbl = Label(frame, text=l, font=("Vrinda (Body CS)", 11), borderwidth=1, relief=SUNKEN, anchor="center", justify="center")
lbl.grid(row=0, column=0, sticky="nsew")
lb = Listbox(frame, font=("Vrinda (Body CS)", 9), height=5, borderwidth=1, selectborderwidth=1, exportselection=FALSE)
lb.grid(row=1, column=0, sticky="nsew")
self.lists.append(lb)
lb.bind('<B1-Motion>', lambda e, s=self: s._select(e.y))
lb.bind('<Button-1>', lambda e, s=self: s._select(e.y))
lb.bind('<Leave>', lambda e: 'break')
lb.bind('<B2-Motion>', lambda e, s=self: s._b2motion(e.x, e.y))
lb.bind('<Button-2>', lambda e, s=self: s._button2(e.x, e.y))
lb.bind('<Button-4>', lambda e, s=self: s._scroll(SCROLL, 1, PAGES))
lb.bind('<Button-5>', lambda e, s=self: s._scroll(SCROLL, -1, PAGES))
lb.bind("<MouseWheel>", self.OnMouseWheel)
sb_y = Scrollbar(self, orient=VERTICAL, command=self._yscroll)
sb_y.grid(row=1, rowspan=2, column=i, sticky="nsew")
self.lists[0]['yscrollcommand'] = sb_y.set
sb_x = Scrollbar(self, orient=HORIZONTAL, command=self._xscroll)
sb_x.grid(row=3, column=0, columnspan=i, sticky="ew")
self.lists[0]['xscrollcommand'] = sb_x.set
def _button2(self, x, y):
for l in self.lists: l.scan_mark(x, y)
return 'break'
def _b2motion(self, x, y):
for l in self.lists: l.scan_dragto(x, y)
return 'break'
def _yscroll(self, *args):
for l in self.lists:
l.yview(*args)
def _xscroll(self, *args):
for l in self.lists:
l.xview(*args)
def curselection(self):
return self.lists[0].curselection()
def delete(self, first, last=None):
for l in self.lists:
l.delete(first, last)
def get(self, first, last=None):
result = []
for l in self.lists:
result.append(l.get(first,last))
if last: return list(map(*[None] + result))
return result
def index(self, index):
self.lists[0].index(index)
def insert(self, index, *elements):
for e in elements:
i = 0
for l in self.lists:
l.insert(index, e[i])
i = i + 1
def size(self):
return self.lists[0].size()
def see(self, index):
for l in self.lists:
l.see(index)
def _select(self, y):
row = self.lists[0].nearest(y)
self.selection_clear(0, END)
self.selection_set(row)
return 'break'
def selection_anchor(self, index):
for l in self.lists:
l.selection_anchor(index)
def selection_clear(self, first, last=None):
for l in self.lists:
l.selection_clear(first, last)
def selection_includes(self, index):
return self.lists[0].selection_includes(index)
def selection_set(self, first, last=None):
self.select_index =[first]
for l in self.lists:
l.selection_set(first, last)
def OnMouseWheel(self, event):
for l in self.lists:
l.yview("scroll", event.delta,"units")
# this prevents default bindings from firing, which
# would end up scrolling the widget twice
return "break"
class FormAddProduct:
def __init__(self, master):
self.frame = master
self.frame.configure(padx=5)
for i in range(0, 1):
self.frame.grid_columnconfigure(i, weight=1)
self.frame.grid_rowconfigure (1, weight = 1)
self._init_widgets()
def _init_widgets(self):
self.frame1 = tk.Frame(self.frame, relief=FLAT, borderwidth=1)
for i in range(0, 2):
self.frame1.grid_columnconfigure(i, weight=1)
for i in range(0, 4):
self.frame1.grid_rowconfigure(i, weight=1)
lbl = Label(self.frame1, text="Product Name").grid(row=0, column=0)
self.ent_item_code = Entry(self.frame1)
self.ent_item_code.grid(row=0, column=1, sticky="nesw")
lbl = Label(self.frame1, text="Amount").grid(row=1, column=0)
self.ent_item_code = Entry(self.frame1)
self.ent_item_code.grid(row=1, column=1, sticky="nesw")
self.mlb = MultiListbox(self.frame1, (
('Product Name', 3), ('Amount $', 1)))
self.mlb.grid(row=2, column=0, columnspan=2, sticky="nsew")
a1 = ("Apple", 10)
a2 = ("Orange", 25)
a3 = ("Banana", 25)
self.mlb.insert(END, a1)
self.mlb.insert(END, a2)
self.mlb.insert(END, a3)
self.frame1.grid(row=1, column=0, sticky="nsew", padx=1, pady=5)
def main():
app = Tk()
FormAddProduct(app)
app.mainloop()
if __name__=='__main__':
main()
【问题讨论】:
标签: tkinter python-3.7