从tkinter.Listbox提取项目列表

太阳熊

我编写了以下tkinter脚本,以了解如何将数据列表添加到tkinter.Listbox小部件中。我发现了两种方法。

接下来,我想从tkinter.Listbox小部件中提取相同的列表。在4种不同的方法中,我仅设法使第4种方法(即e4)起作用。

我如何才能使用方法e1,e2和e3?最终目标是获取最初提供给tkinter.Listbox小部件的列表。

测试脚本:

import tkinter as tk # Python 3 tkinter modules
import tkinter.ttk as ttk

class App(ttk.Frame):
    def __init__(self, parent, *args, **kwargs):
        # 1. Initialise Frame
        ttk.Frame.__init__(self, parent)
        self.parent = parent

        # Method1
        name1 = ['Peter', 'Scotty', 'Walter', 'Scott', 'Mary']
        self.lb1_values = tk.StringVar(value=name1)
        self.listbox1 = tk.Listbox(self, listvariable=self.lb1_values)

        # Method2
        self.listbox2 = tk.Listbox(self)
        name2 = ['Sarah', 'Sean', 'Mora', 'Mori', 'Mary']
        for item in name2:
            self.listbox2.insert(tk.END, item)

        self.listbox1.grid(in_=self, row=0, column=0, sticky='nsew')
        self.listbox2.grid(in_=self, row=0, column=1, sticky='nsew')

        # Extract values from listbox and covert to a list
        e1 = self.lb1_values.get()
        print('e1 = ', e1)
        print('type(e1) = ', type(e1))
        e1 = e1.strip(',')
        print('e1 = ', e1)

        e2 = self.listbox1.cget('listvariable')
        print('\ne2 = ', e2)
        print('type(e2) = ', type(e2))
        e2 = e2.split(',')
        print('e2 = ', e2)

        e3 = self.listbox2.cget('listvariable')
        print('\ne3 = ', e3)
        print('type(e3) = ', type(e3))

        e4 = self.listbox2.get(0, tk.END)
        print('\ne4 = ', e4)
        print('type(e4) = ', type(e4))
        e4 = list(e4)
        print('e4 = ', e4)    


if __name__ == '__main__':
    root = tk.Tk()
    root.title('App'), root.geometry('400x200')
    app = App(root)
    app.grid(row=0, column=0, sticky='nsew')
    #root.mainloop()

输出:

e1 =  ('Peter', 'Scotty', 'Walter', 'Scott', 'Mary')
type(e1) =  <class 'str'>
e1 =  ('Peter', 'Scotty', 'Walter', 'Scott', 'Mary')

e2 =  PY_VAR0
type(e2) =  <class 'str'>
e2 =  ['PY_VAR0']

e3 =  
type(e3) =  <class 'str'>

e4 =  ('Sarah', 'Sean', 'Mora', 'Mori', 'Mary')
type(e4) =  <class 'tuple'>
e4 =  ['Sarah', 'Sean', 'Mora', 'Mori', 'Mary']
布莱恩·奥克利(Bryan Oakley)

您不能将StringVar用作listvariable属性的目标如您的代码所示,这将导致列表转换为字符串。

但是,您可以使用的实例tk.Variable代替。Variable是的基类StringVar的基本实现get不会将值强制转换为字符串。

name1 = ['Peter', 'Scotty', 'Walter', 'Scott', 'Mary']
self.lb1_values = tk.Variable(value=name1)
self.listbox1 = tk.Listbox(self, listvariable=self.lb1_values)
...
e1 = self.lb1_values.get()
print('e1 = ', e1)
print('type(e1) = ', type(e1))
print('e1 = ', e1)

上面产生了这个输出:

e1 =  ('Peter', 'Scotty', 'Walter', 'Scott', 'Mary')
type(e1) =  <class 'tuple'>
e1 =  ('Peter', 'Scotty', 'Walter', 'Scott', 'Mary')

对于e2和e3,您必须跳过一个额外的箍。cget不幸的是,方法返回内部变量名称,而不是对变量对象的引用。要通过名称获取变量的值,您需要使用widget方法getvar

例如:

e2 = self.listbox1.cget('listvariable')
print('\ne2 = ', e2)
print('type(e2) = ', type(e2))
print('e2 = ', self.getvar(e2))

上面产生了这个输出:

e2 =  PY_VAR0
type(e2) =  <class 'str'>
e2 =  ('Peter', 'Scotty', 'Walter', 'Scott', 'Mary')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章