2017-03-07 14 views
0

私はTkinterの新機能です。スクロールバーの両側にテキストを表示

私は、スクロールバーの左側にユーザ​​ーのクエリを、右側にシステムの応答を表示するチャットシステムを構築しようとしています。それは可能ですか?

現在、すべて1つのside.Thisに来ては、コードがある

ようscrollviewがどのように見えるかです:

from Tkinter import * 
import Tkinter as ttk 
from ttk import * 

master = Tk() 

rectangleFrame = Frame(master) 
rectangleFrame.grid(column =50, row = 50, sticky=(N,W,E,S)) 
rectangleFrame.columnconfigure(0, weight = 1) 
rectangleFrame.rowconfigure(0, weight = 1) 
rectangleFrame.pack(pady = 10, padx = 10) 

def getEdittextValue(*args): 
    listbox.insert(END, "You: Something") 
    listbox.itemconfig(END, {'bg':'red', 'fg':'black'}) 
    listbox.insert(END, "Bot: something else") 
    listbox.itemconfig(END, {'bg':'grey', 'fg':'blue'}) 


scrollbar = Scrollbar(rectangleFrame, width = 30) 
scrollbar.grid(sticky="NWEW") 
scrollbar.pack(side="right", fill="y", expand=False) 

listbox = Listbox(rectangleFrame) 
listbox.pack(side="left", fill="both", expand=True) 


listbox.config(yscrollcommand=scrollbar.set) 
scrollbar.config(command=listbox.yview) 
query_button = Button(rectangleFrame, command=getEdittextValue, text = "Process") 
query_button.pack() 
rectangleFrame.pack(fill=BOTH, expand=YES) 

master.mainloop() 

は、私は、関数で2つの挿入をしています。 1つはユーザクエリであり、もう1つはシステムの応答です。

+0

、私たちはあなたの例に基づいたソリューションを与えるために、私たちのコンピュータ上でそれを試すことができるように、(輸入し、残りの必要なコードを追加します)あなたのコードの_runnable_を作るためにあなたの投稿を編集してください。 – nbro

+0

コードを編集しました –

+0

希望の例を表示できますか? – Novel

答えて

1

スクロールバーでクエリとレスポンスを区切りたい場合は、2つのリストボックスを使用する必要があります。それらを一緒にスクロールする私のコードはhttp://effbot.org/tkinterbook/listbox.htmに基づいていて、マウスホイールと一緒にスクロールしたい場合は、Scrolling multiple Tkinter listboxes togetherの答えを参照してください。

互換性のないパックおよびグリッドレイアウト(たとえば、rectangleFrame用)が混在しています。 1つを選択してそれに固執する必要があります。自分のコードにpackを使用しました。

import Tkinter as tk 
import ttk 

master = tk.Tk() 

rectangleFrame = ttk.Frame(master) 
rectangleFrame.pack(pady=10, padx=10, fill="both", expand=True) 

count = 0 # query counter to see that both listboxes are scrolled together 

def getEdittextValue(): 
    global count 
    listbox_query.insert("end", "You: query %i" % count) 
    listbox_query.itemconfig("end", {'bg':'red', 'fg':'black'}) 
    listbox_response.insert("end", "Bot:response %i" % count) 
    listbox_response.itemconfig("end", {'bg':'grey', 'fg':'blue'}) 
    count += 1 

def yview(*args): 
    """ scroll both listboxes together """ 
    listbox_query.yview(*args) 
    listbox_response.yview(*args) 

scrollbar = ttk.Scrollbar(rectangleFrame) 
listbox_query = tk.Listbox(rectangleFrame) 
listbox_response = tk.Listbox(rectangleFrame) 

scrollbar.config(command=yview) 
listbox_query.config(yscrollcommand=scrollbar.set) 
listbox_response.config(yscrollcommand=scrollbar.set) 

query_button = ttk.Button(rectangleFrame, command=getEdittextValue, text="Process") 

listbox_query.pack(side="left", fill="both", expand=True) 
scrollbar.pack(side="left", fill="y") 
listbox_response.pack(side="left", fill="both", expand=True) 
query_button.pack(side="left") 

master.mainloop() 
関連する問題