Python tkinter:在列表框中添加垂直和水平滚动条

时间:2018-08-24 04:35:37

标签: python python-3.x python-2.7 tkinter

我在Python中有这个render_gui函数。

def render_gui():
    global root
    global prev_key
    global listbox, label1, entry1
    prev_key = 0

    root = tk.Tk()
    root.title('Server Chat Application')

    scroll = Scrollbar(root)
    scroll.pack(side = "right" , fill= "y")
    listbox = Listbox(root, width=80, height=30)

    listbox.grid(row=1,column=1, columnspan=5,rowspan=3)    

    scroll.config(command = listbox.yview)

    label1 = ttk.Label(root, text='Message Entry Box : ')
    label1.grid(row=4,column=1)
    entry1 = ttk.Entry(root, width=60)
    entry1.grid(row=4, column=2)    

    root.geometry('490x520')
    #print('before main loop')
    root.bind('<KeyPress>', onKeyPress)
    #print('after main loop')
    root.mainloop()

添加滚动条后,它抛出了错误。如何将垂直和水平滚动条合并到列表框中。谢谢。

更新:

下面是完整的代码。我已经对其进行了编辑,并省略了打包功能,将其替换为网格功能,但未显示滚动条。我该怎么处理?

import os
import signal
import socket
import time
import threading
import keyboard
from threading import Thread


import tkinter as tk
from tkinter import ttk
from tkinter import *
import webbrowser

def onKeyPress(event):
    global prev_key
    global root
    global listbox, label1, entry1


    if(event.keycode == 13):        
        print(Entry.get(entry1))
        msg = Entry.get(entry1)
        if len(str.encode(msg)) > 0:
            client.send(str.encode("\nserver > " + msg))
            listbox.insert(END, "server > " + msg)
            entry1.delete(0,END)

    if(event.keycode == 88) and (prev_key == 17):        
        root.destroy()
        try:            
            raise client.send(str.encode("***Server has been shutdowned***"))
        except:
            print ("You are not yet connected to any client to send shutdown notice.")
        sys.exit()
        os.kill(os.getpid(), signal.SIGUSR1)

    prev_key = event.keycode

def render_gui():
    global root
    global prev_key
    global listbox, label1, entry1
    prev_key = 0

    root = tk.Tk()
    root.title('Server Chat Application')

    scroll = Scrollbar(root)
    #scroll.pack(side = "right" , fill= "y")
    scroll.grid(row=0, column=10, rowspan=10, sticky='ns')
    listbox = Listbox(root, width=80, height=30)   



    listbox.grid(row=1,column=1, columnspan=5,rowspan=3)
    scroll.config(command = listbox.yview)
    listbox.config(yscrollcommand=scroll.set)


    label1 = ttk.Label(root, text='Message Entry Box : ')
    label1.grid(row=4,column=1)
    entry1 = ttk.Entry(root, width=60)
    entry1.grid(row=4, column=2)    

    root.geometry('490x520')
    #print('before main loop')
    root.bind('<KeyPress>', onKeyPress)
    #print('after main loop')
    root.mainloop()

if __name__ == "__main__":
    Thread(target= render_gui).start()

0 个答案:

没有答案