2017-03-07 64 views
0

我對Tkinter很新穎。顯示滾動條兩邊的文字

我想建立一個聊天系統,我想在滾動條的左側顯示用戶的查詢,並在右側顯示系統響應。可以做到嗎?

目前,一切都在一個side.This是滾動型的樣子

的代碼是:

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個插入。一個用戶查詢,另一個用系統響應。

+0

請編輯您的帖子使你的代碼_runnable_(添加進口,其餘必要的代碼),這樣我們就可以嘗試一下我們的電腦,爲了讓您根據您的例子的解決方案。 – nbro

+0

我編輯了代碼 –

+0

你能舉一個你想要的例子嗎? – Novel

回答

1

如果您希望查詢和響應由滾動條分隔,則需要使用2個列表框。我的代碼滾動到一起是基於http://effbot.org/tkinterbook/listbox.htm,如果您還想用鼠標滾輪將它們一起滾動,請參閱此問題的答案:Scrolling multiple Tkinter listboxes together

您一直在混合包裝和網格佈局(例如rectangleFrame),它們是不兼容的。你需要選擇一個並堅持下去。我在我的代碼中使用了包。

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()