how to fix the position of scrollbar of scrolledtext widget in tkinter? - python

i want to fix the postion of scrollbar of scrolledtext widget of tkinter.
i'am creating a chatbot where after every new message there is need to drag the scrollbar down to see conversation which has a bad impact.
here is the code of scrolledtext
self.conversation = ScrolledText.ScrolledText(self,
state='disabled',borderwidth=5,
highlightthickness=1,
bg='#15202b',fg='#16202A',
font=('Arial Bold',8))
self.conversation.grid(column=0, row=1, columnspan=2, sticky='nesw', padx=3, pady=3)

from tkinter import *
from tkinter import scrolledtext
root = Tk()
scroll_x = Scrollbar(root, orient="horizontal")
text = scrolledtext.ScrolledText(root, wrap=NONE)
text.config(xscrollcommand=scroll_x.set)
scroll_x.configure(command=text.xview)
text.pack(fill=X)
scroll_x.pack(fill=X)
for i in range(10000):
text.insert(END, str(i)+"\n")
text.see("end")
root.update()
root.mainloop()
The command you are looking for is see("end")
text.see("end")
root.update()

Related

TKinter - Unable to update Text widget

I am trying to update a Text widget, but it's not updated no matter what I try, there is no error as well
def update():# a button calls this
textBox.delete(1.0, tk.END)
textBox.insert(tk.END,"test")
textBox = tk.Text(frame1,height=2,width=10)
textBox.config(state='disabled') #disable editing
textBox.grid(row=0,column=1,pady=2)
Using #JacksonPro's suggestion
import tkinter as tk
def update():# a button calls this
textBox.config(state="normal") # Make the state normal
textBox.delete("0.0", "end")
textBox.insert("end", "test")
textBox.config(state="disabled") # Make the state disabled again
root = tk.Tk()
textBox = tk.Text(root, height=2, width=10)
textBox.config(state="disabled") #disable editing
textBox.grid(row=0, column=1, pady=2)
button = tk.Button(root, text="Click me", command=update)
button.grid(row=1, column=1)
root.mainloop()

tkinter centering two or more widgets

So I created a frame in which I want to put two widgets with them being centered on the x-axis. When it's one object pack() centers it automatically. But I can't figure out two widgets. I tried with grid() but there is free space left on the right which makes it look unsymetrical as seen in the image.
how could I get what is seen on the right? (I'd prefer it being dont with pack() but if there is a solution with grid() and/or place() as well i'd appreciate those as well!)
here's the code for the left picture
from tkinter import *
from tkinter import font
root = Tk()
root.geometry("500x500")
frame = Frame(root, bg="white", highlightbackground="black", highlightthickness=2)
frame.place(relwidth=0.5, relheight=0.5, relx=0.5, rely=0.5, anchor=CENTER)
label = Label(frame, bg="lime", text="label", font=font.Font(size=20))
label.grid(column=0, row=0)
button = Button(frame, bg="yellow", text="pressbutton", font=font.Font(size=20))
button.grid(column=1, row=0)
root.mainloop()
You can use frame.pack() to easily position the frame in the top, middle of its parent.
from tkinter import *
from tkinter import font
root = Tk()
root.geometry("500x500")
frame = Frame(root, bg="white", highlightbackground="black", highlightthickness=2)
frame.pack()
label = Label(frame, bg="lime", text="label", font=font.Font(size=20))
label.grid(column=0, row=0)
button = Button(frame, bg="yellow", text="pressbutton", font=font.Font(size=20))
button.grid(column=1, row=0)
root.mainloop()
You can put the label and button in another frame, and use pack() on that frame:
from tkinter import *
from tkinter import font
root = Tk()
root.geometry("500x500")
frame = Frame(root, bg="white", highlightbackground="black", highlightthickness=2)
frame.place(relwidth=0.5, relheight=0.5, relx=0.5, rely=0.5, anchor=CENTER)
frame2 = Frame(frame)
frame2.pack() # default side='top'
label = Label(frame2, bg="lime", text="label", font=font.Font(size=20))
label.pack(side='left', fill='both')
button = Button(frame2, bg="yellow", text="pressbutton", font=font.Font(size=20))
button.pack(side='left')
root.mainloop()

Tkinter scrollbars not filling or aligning correctly

I'm trying to create a frame with both a vertical and horizontal scrollbar, but the horizontal one seems to pack next to the canvas, and not below it. I have the scrollbar packed with side=tk.BOTTOM and fill=tk.X, so I'm not sure what else I need to add. What should I do to get the horizontal scrollbar to stretch across the entire canvas?
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
frame=tk.Frame(root)
frame.pack()
canvas=tk.Canvas(frame, height=200, width=200, background="blue")
canvas.pack(side=tk.LEFT)
yscrollbar=tk.Scrollbar(frame, orient=tk.VERTICAL, command=canvas.yview)
yscrollbar.pack(side=tk.RIGHT, fill=tk.Y)
xscrollbar=tk.Scrollbar(frame, orient=tk.HORIZONTAL, command=canvas.xview)
xscrollbar.pack(side=tk.BOTTOM, fill=tk.X)
canvas.configure(yscrollcommand=yscrollbar.set)
canvas.configure(xscrollcommand=xscrollbar.set)
root.mainloop()
Right now the canvas is set to side=tk.LEFT. If I set it to side=tk.TOP, the reverse problem happens.
The issue comes from the order in which you pack the widgets. The idea is that pack uses the remaining space to put what is left, so you need to pack first the scrollbars, then at last the canvas:
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
frame=tk.Frame(root)
frame.pack()
canvas=tk.Canvas(frame, height=200, width=200, background="blue")
yscrollbar=tk.Scrollbar(frame, orient=tk.VERTICAL, command=canvas.yview)
xscrollbar=tk.Scrollbar(frame, orient=tk.HORIZONTAL, command=canvas.xview)
xscrollbar.pack(side=tk.BOTTOM, fill=tk.X)
yscrollbar.pack(side=tk.RIGHT, fill=tk.Y)
canvas.pack(side=tk.LEFT)
canvas.configure(yscrollcommand=yscrollbar.set)
canvas.configure(xscrollcommand=xscrollbar.set)
root.mainloop()
However, because pack is not so intuitive when it comes to more complex GUI, I prefer to use grid:
xscrollbar.grid(row=1, column=0, sticky='ew')
yscrollbar.grid(row=0, column=1, sticky='ns')
canvas.grid(row=0, column=0, sticky='ewns')
But in this case, if you want your GUI to resize properly, you will need to add
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
so that the row and column 0 of the grid will fill all the available space.
In my env, calling canvas.pack and frame.pack after defining y and xscrollbar worked fine.
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
frame=tk.Frame(root)
yscrollbar=tk.Scrollbar(frame, orient=tk.VERTICAL, command=canvas.yview)
yscrollbar.pack(side=tk.RIGHT, fill=tk.Y)
xscrollbar=tk.Scrollbar(frame, orient=tk.HORIZONTAL, command=canvas.xview)
xscrollbar.pack(side=tk.BOTTOM, fill=tk.X)
canvas=tk.Canvas(frame, height=200, width=200, background="blue")
canvas.pack(side=tk.LEFT)
frame.pack()
canvas.configure(yscrollcommand=yscrollbar.set)
canvas.configure(xscrollcommand=xscrollbar.set)
root.mainloop()

Tkinter Frame Disappears after adding a button [duplicate]

This question already has an answer here:
How to stop Tkinter Frame from shrinking to fit its contents?
(1 answer)
Closed 4 years ago.
I have two frames in the root. And I want to add a button in one of the frames. Both frames have different background colours. When I try to add a button in any of them, the frame that contains the button disappears.
Without button
from tkinter import *
root = Tk()
root.geometry("1600x800+0+0")
root.title("ABC")
Rf = Frame(root, width=100, height=800, bg="black")
Rf.pack(side=RIGHT)
Lf = Frame(root, width=1500, height=800, bg="green")
Lf.pack(side=LEFT)
root.mainloop()
Which results in...
With button
But after adding the button, with the following code...
from tkinter import *
root = Tk()
root.geometry("1600x800+0+0")
root.title("ABC")
Rf = Frame(root, width=100, height=800, bg="black")
Rf.pack(side=RIGHT)
Lf = Frame(root, width=1500, height=800, bg="green")
Lf.pack(side=LEFT)
b1 = Button (Rf, text="Load", fg="red", bg="black")
b1.pack(side=LEFT)
root.mainloop()
I get...
Now the button is visible but the frame and background colours are gone. What am I doing wrong?
Thanks for your help!
Your frame is actually disappearing it is just resize and that is why you cant see it. Add Rf.pack_propagate(False) to your frame it will prevent the frame from resizing when a new widget is added.
from tkinter import *
root= Tk()
root.geometry("1600x800+0+0")
root.title("ABC")
Rf=Frame(root,width=100, height=800, bg="black")
Rf.pack_propagate(False)
Rf.pack(side=RIGHT)
Lf=Frame(root,width=1500, height=800, bg="green")
Lf.pack(side=LEFT)
b1 = Button (Rf, text= "Load", fg= "red", bg="black")
b1.pack(side=LEFT)
root.mainloop()

Tkinter in fullscreen mode makes Text widget not work

When using Tkinter as a regular window, the text widget works as intended and can be typed in.
from Tkinter import *
root = Tk()
T = Text(root, height=2, width=30)
T.pack()
T.insert(END, "Just a text Widget\nin two lines\n")
mainloop()
However if we make this application fullscreen:
from Tkinter import *
root = Tk()
root.attributes("-fullscreen", True)
T = Text(root, height=2, width=30)
T.pack()
T.insert(END, "Just a text Widget\nin two lines\n")
mainloop()
And try to type in the textbox, it won't work and the keys will instead appear in the terminal. Why is this and is there a solutoin?
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes('-fullscreen',True)
canvas= Canvas(root)
canvas.create_text(300, 50,text="This is a window", fill="black", font=('Helvetica 15 bold'))
canvas.pack()
This seems to be working with me

Categories