2017-03-07 48 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()