2017-11-21 11 views
-1

2つのスレッドがあります。 1つは番号を提供し、もう1つはtkinterを使用して、それが生成されるとその番号をキャッチします。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

[?Tkinterのリストボックスの選択が変更されたコールバックを取得する](https://stackoverflow.com/questions/6554805/getting-a-callback-when-a-tkinter-listbox -selection-is-changed) – furas

+0

@furasはい私はそれを達成したいと思います。なぜ私は 'バインド'を使用すると動作しません。 – user6456568

+0

あなたはフラスによってリンクされた質問の2番目の答えを試しましたか? –

答えて

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() 
関連する問題