Add a scrollbar for text object inside frame - python

I have a frame which has a text object inside it. How do I add a vertical scrollbar that controls the text object. The scrollbar should be on the right side..
inputFrame = Frame(bg='#d9d9d9')
inputFrame.pack()
inputEntryImg = PhotoImage(file=resource_path("inputEntry.png"))
inputEntryBg = mainWindowCanvas.create_image(400.0, 168.5, image=inputEntryImg)
inputEntry = Text(inputFrame, bd=0, bg="#d9d9d9", highlightthickness=0, font='calibri',
pady=10)
inputEntry.pack(padx=(0, 10), pady=10)
inputFrame.place(x=41.0, y=83, width=718.0, height=169)
Thankyou.

Try this
scrollbar = Scrollbar(inputFrame, orient=VERTICAL, command=inputEntry.yview)
scrollbar.pack(side=RIGHT, fill=Y)
inputEntry.config(yscrollcommand=scrollbar.set)

How do I add a vertical scrollbar that controls the text object. The
scrollbar should be on the right side..
Try this. I did not add PhotoImage. You can add it by yourself.
Code:
import tkinter as tk
root = tk.Tk()
inputFrame = tk.Frame(bg='#d9d9d9')
inputFrame.pack()
#inputEntryImg = tk.PhotoImage(file=resource_path("inputEntry.png"))
#inputEntryBg = mainWindowCanvas.create_image(400.0, 168.5, image=inputEntryImg)
inputEntry=tk.Text(inputFrame, height=10, width=50)
scrollBar= tk.Scrollbar(inputFrame, command=inputEntry.yview, orient="vertical")
scrollBar.grid(row=0, column=1, sticky="ns")
inputEntry.grid(row=0,column=0)
inputEntry.configure(yscrollcommand=scrollBar.set)
root.mainloop()
Screenshot for scrollbar with text:

Related

Tkinter: Placing widgets on top of other Widgets

I'm trying to place an Entry widget on top of a frame but i'm having some trouble with it
(I'm using customtk but i suppose there's no big difference)
import customtkinter
app = customtkinter.CTk()
app.geometry(500x380)
random_frame = customtkinter.CTkFrame(master=app, width=480, height=50)
Box1 = customtkinter.CTkEntry(master=app, width=30, height=30)
random_frame.grid(row=3, column=0, padx=10, pady=10)
Box1.grid(row=3, column=1, padx=10, pady=10)
I am expecting the Box1 widget to be on top of the random_frame widget, however in the gui i can't see the Box1. I'm unsure if it's under the frame, but that's what i think is happening.
I tried using the lift() method but it doesn't seem to do anything

Scroll bars not working correctly with canvas

The scroll bars move the canvas but they always snap back to the top or the left. What am I doing wrong?
import tkinter as Tk
root = Tk.Tk()
root.rowconfigure(0,weight=1)
root.columnconfigure(0,weight=1)
frame = Tk.Frame(root)
frame.grid(row=0, column=0, sticky='NSEW')
frame.rowconfigure(0,weight=1)
frame.columnconfigure(0,weight=1)
canvas = Tk.Canvas(frame)
canvas.grid(row=0, column=0, sticky='NSEW')
scroll_x = Tk.Scrollbar(frame, orient="horizontal", command=canvas.xview)
scroll_x.grid(row=1, column=0, sticky="ew")
scroll_y = Tk.Scrollbar(frame, orient="vertical", command=canvas.yview)
scroll_y.grid(row=0, column=1, sticky="ns")
canvas.create_oval(0,0,1333,1000)
canvas.configure(scrollregion=canvas.bbox("all"))
root.mainloop()
Scrollbars require two-way communication with the widgets they are controlling. You've properly configured the scrollbars but haven't configured the canvas to update the scrollbars.
To do that, add the following code after you've defined the canvas and scrollbars:
canvas.configure(yscrollcommand=scroll_y.set, xscrollcommand=scroll_x.set)

How can I change the shape of the scrollbar in tkinter, python?

I use scrollbar widget and I want to change the shape of it.
Here's my questions below.
Can I use the image on the scroll bar?
How can I modify the color and shape with the options like relief, background, highlightbackground, highlightcolor or highlightthickness? I already tried but not a thing has changed.
from tkinter import *
root = Tk()
root.geometry("640x480")
frame = Frame(root)
frame.pack()
scrollbar = Scrollbar(frame, relief=RAISED, width=20, bd=5, activebackground="yellow", elementborderwidth=10, troughcolor="yellow", highlightbackground="red")
scrollbar.pack(side="right", fill="y")
listbox = Listbox(frame, selectmode="extended", height=10, bg="green", fg="white", yscrollcommand=scrollbar.set)
for i in range(1, 32):
listbox.insert(END, str(i) + "day")
listbox.pack()
scrollbar.config(command = listbox.yview) # scrollbar와 listbox를 mapping 해줌
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()

Python tkiner, two text boxes with x and y scrollbars for both

I am trying to make a gui that has two separate text outputs with horizontal and vertical scollbars for each text box that are fixed to the right and bottom edges of each respective text windows. I am struggling with how to do this with the tkinter grid and any help would be appreciated.
import tkinter as tk
class WeatherGUI(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
# Horizontal (x) Scroll bar
self.xscrollbar = tk.Scrollbar(self, orient="horizontal")
self.xscrollbar.grid(column=5, row=10, sticky="we")
# Vertical (y) Scroll Bar
self.yscrollbar = tk.Scrollbar(self)
self.yscrollbar.grid(column=5, row=10, sticky='ns')
self.xscrollbar2 = tk.Scrollbar(self, orient="horizontal")
self.xscrollbar2.grid(column=9, row=10, sticky="we")
# Vertical (y) Scroll Bar
self.yscrollbar2 = tk.Scrollbar(self)
self.yscrollbar2.grid(column=9, row=10, sticky='ns')
self.NSW_actual_text = tk.Text(self, width=50, wrap = "none", xscrollcommand=self.xscrollbar.set, yscrollcommand=self.yscrollbar.set,)
self.NSW_actual_text.grid(column=0, columnspan=4, row= 8,padx=(20, 10))
self.NSW_forecast_text = tk.Text(self, width=50, wrap = "none", xscrollcommand=self.xscrollbar.set, yscrollcommand=self.yscrollbar.set)
self.NSW_forecast_text.grid(column=8, columnspan=4, row= 8,padx=(20, 10))
self.xscrollbar.config(command=self.NSW_actual_text.xview)
self.yscrollbar.config(command=self.NSW_actual_text.yview)
self.xscrollbar2.config(command=self.NSW_forecast_text.xview)
self.yscrollbar2.config(command=self.NSW_forecast_text.yview)
self.btn1 = tk.Button(self, text="Generate NWS Actual", command=self.GenerateNWSActual)
self.btn1.grid(column=1, row=0)
self.btn2 = tk.Button(self, text="Generate NWS Forecast", command=self.GenerateNWSForecast)
self.btn2.grid(column=10, row=0)
def GenerateNWSActual(self):
self.NSW_actual_text.insert('1.0', "This is where actual weather would go")
def GenerateNWSForecast(self):
self.NSW_forecast_text.insert('1.0', "this is where forecast weather would go")
app = WeatherGUI()
app.mainloop()
The following example allows you to attach two functional scrollbars (x, y) to a Text widget
from tkinter import *
# Create Window
root = Tk()
# Create ScrollBars
xScrollbar = Scrollbar(root, orient=HORIZONTAL)
yScrollbar = Scrollbar(root, orient=VERTICAL)
# Create Text Widget with scroll commands
TextWidget = Text(root, xscrollcommand=xScrollbar, yscrollcommand=yScrollbar)
# Package Componets
xScrollbar.pack(side=BOTTOM, fill=X)
yScrollbar.pack(side=RIGHT, fill=Y)
TextWidget.pack(fill=BOTH, expand=20)
# Assign Scrollbars with TextWidget
xScollbar.config(command=TextWidget.xview)
yScollbar.config(command=TextWidget.yview)
You can use this examble for both of your TextWidgets in your weather application.

Categories