1

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() 
3
  • Your example would be better if you removed the dependency on the external modules. If the question is about how to arrange two sets of widgets and scrollbars, the other modules are irrelevant. Commented Apr 6, 2020 at 3:11
  • You are absolutely right. Should I delete this question and as a more focused one? Commented Apr 6, 2020 at 3:19
  • If the textbox is at (row,col), then put the vertical scrollbar to (row,col+1) and the horizontal scrollbar to (row+1, col) with same columnspan value. Commented Apr 6, 2020 at 3:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.