user3678184
user3678184

Reputation: 105

Can't get this scrollbar to work with Text widet in Tkinter?

I know there are a lot of questions on this but I need some help on getting this to work. Basically there is a Text widget and I want it to have a scroll bar on the right, vertical side of it. I want the scrollbar inside the Text widget and for it to always be visible. Here is my code so far. (BTW for some reason whenever I run stuff with .pack() the python console just freezes? So doing this with .grid() would be appreciated, if not possible thats ok):

console = Text(root, width=35, height=16)
console.grid(row=6, column=0, sticky="w")

scrollbar = Scrollbar(root, command=console.yview)
console.config(yscrollcommand=scrollbar.set)
scrollbar.pack(side="right", fill="y")

Full code:

from Tkinter import *
import socket
import tkFont
#User Interface---------------------------------------
root = Tk()
root.title("MY GUI")
root.geometry("372x414+510+175")
root.resizable(width=FALSE, height=FALSE)
'''FONTS------------------------------------------------'''
label_f = tkFont.Font(family="Trebuchet MS", size="12" )
entry_f = tkFont.Font(family ="Courier", size = "12")
'''IP-----------------------------'''
target = Label(root, text="Target IP Adress:",font= label_f).grid(row=0, column=0, sticky='w')
userIP = StringVar()
tar_ent = Entry(root, width = "33", font=entry_f, textvariable = userIP)
tar_ent.grid(row=1, column=0, sticky='w')
'''Port ------------------------------'''
custom = Label(root, text = "Custom port scanner (Enter like 23,80,etc.)", font=label_f).grid(row=2, column=0, sticky='w')
userPort = StringVar()
cus_port = Entry(root, width="33", font=entry_f, textvariable=userPort)
cus_port.grid(row=3, column=0, sticky='w')
'''Results--------------------------'''
space = Label(root, text="")
space.grid(row=5, column=0)
console = Text(root, width=35, height=16)
console.grid(row=6, column=0, sticky="w")

scrollbar = Scrollbar(root, command=console.yview)
console.config(yscrollcommand=scrollbar.set)
scrollbar.grid(row=6, column=1, sticky='Ns')#side="right", fill="y")

Upvotes: 0

Views: 148

Answers (1)

falsetru
falsetru

Reputation: 369134

Don't mix layout (pack, grid, place) for the same parent widget.

Replace the following line:

scrollbar.pack(side="right", fill="y")

with:

scrollbar.grid(row=6, column=1, sticky='NS')

UPDATE according to the question change:

Here's the full code modified:

from Tkinter import *
import socket
import tkFont
#User Interface---------------------------------------
root = Tk()
root.title("MY GUI")
root.geometry("372x414+510+175")
root.resizable(width=FALSE, height=FALSE)
'''FONTS------------------------------------------------'''
label_f = tkFont.Font(family="Trebuchet MS", size="12" )
entry_f = tkFont.Font(family ="Courier", size = "12")
'''IP-----------------------------'''
target = Label(root, text="Target IP Adress:",font= label_f).grid(row=0, column=0, sticky='w')
userIP = StringVar()
tar_ent = Entry(root, width = "33", font=entry_f, textvariable = userIP)
tar_ent.grid(row=1, column=0, sticky='WE', columnspan=2)
'''Port ------------------------------'''
custom = Label(root, text = "Custom port scanner (Enter like 23,80,etc.)", font=label_f).grid(row=2, column=0, sticky='w')
userPort = StringVar()
cus_port = Entry(root, width="33", font=entry_f, textvariable=userPort)
cus_port.grid(row=3, column=0, sticky='WE', columnspan=2)
'''Results--------------------------'''
space = Label(root, text="")
space.grid(row=5, column=0)
console = Text(root, width=35, height=16)
console.grid(row=6, column=0, sticky="WE")

scrollbar = Scrollbar(root, command=console.yview)
console.config(yscrollcommand=scrollbar.set)
scrollbar.grid(row=6, column=1, sticky='Ns')

root.mainloop()

enter image description here

Upvotes: 2

Related Questions