如何使用tkinter在滚动条中显示列表框中所选项目的详细信息

时间:2017-11-21 09:22:40

标签: python tkinter listbox

有2个主题。一个是提供数字,另一个是使用tkinter的数字,一旦产生数字就会捕获数字。

我使用滚动条作为数字序列。但是,我想看一些我点击的细节,比如方形数字。

例如,如果我在结果中单击8,我可以获得64个详细信息

我的想法的图片

那么如何实现呢?我目前的代码在这里:

from concurrent.futures import ThreadPoolExecutor
from tkinter import *
from time import sleep
global root
global scrollbar
global listb

def my_func1():
    global root
    global Scrollbar
    root = Tk()
    lbl1 = Label(root, text="Result:", fg='black',
                 font=("Helvetica", 16, "bold"))
    lbl2 = Label(root, text="Detail:", fg='black',
                 font=("Helvetica", 16, "bold"))
    lbl1.grid(row=0, column=0,  sticky=W)
    lbl2.grid(row=2, column=0,  sticky=W)

    frm = Frame(root)
    frm.grid(row=1, columnspan=2, sticky=N + S + W + E)

    try:
        scrollbar = Scrollbar(frm, orient=VERTICAL)
        global listb
        listb = Listbox(frm, yscrollcommand=scrollbar.set, width=50)
        scrollbar.config(command=listb.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        listb.pack(fill=BOTH, expand=YES)
        detail = Listbox(root, height=10, font=("Helvetica", 12))
        detail.grid(row=6, columnspan=2, sticky=E + W + N)
    except:
        pass
    root.mainloop()

def my_func2():
    global listb
    sleep(0.1)
    for i in range(1000):
        sleep(0.3)
        vw = listb.yview()
        listb.insert(END, i)
        listb.yview_moveto(vw[-1])
        print(i)
executors_list = []

with ThreadPoolExecutor(max_workers=5) as executor:
    executors_list.append(executor.submit(my_func1))
    executors_list.append(executor.submit(my_func2))

1 个答案:

答案 0 :(得分:0)

感谢大家的帮助。答案在这里:

import time, threading
from time import sleep
from tkinter import *
from tkinter import messagebox
global listb

class App(object):
    def __init__(self, root):
        def onselect(evt):
            w = evt.widget
            index = int(w.curselection()[0])
            value = w.get(index)
            detail.delete('1.0', END)
            detail.insert(END,str(value*value))
        lbl1 = Label(root, text="Result:", fg='black',
                    font=("Helvetica", 16, "bold"))
        lbl2 = Label(root, text="Detail:", fg='black',
                        font=("Helvetica", 16, "bold"))
        lbl1.grid(row=0, column=0,  sticky=W)
        lbl2.grid(row=2, column=0,  sticky=W)

        frm = Frame(root)
        frm.grid(row=1, columnspan=2, sticky=N + S + W + E)


        scrollbar = Scrollbar(frm, orient=VERTICAL)
        global listb
        listb = Listbox(frm, yscrollcommand=scrollbar.set, width=50)
        scrollbar.config(command=listb.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        listb.pack(fill=BOTH, expand=YES)

        detail = Text(root, height=10, font=("Helvetica", 12))
        detail.grid(row=6, columnspan=2, sticky=E + W + N)
        listb.bind('<<ListboxSelect>>', onselect)


def InfiniteProcess():
    global listb
    sleep(0.1)
    for i in range(1000):
        sleep(0.3)
        vw = listb.yview()
        listb.insert(END, i)
        listb.yview_moveto(vw[-1])
        #print(i)

finish = False
Process = threading.Thread(target=InfiniteProcess)
Process.start()

mainWindow = Tk()
app = App(mainWindow)
mainWindow.mainloop()
finish = True
Process.join()