Python Forum
[Tkinter] Scrollable buttons with an add/delete button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Scrollable buttons with an add/delete button
#1
I am trying to make a GUI to manage the sensors I am going to be putting in my aquariums, terrariums, and vivariums. I am going to have a main menu screen which will have a tab where I can see sensor info. My problem is I can not seem to figure out a way to make my sensor "rows" scrollable. What I have in mind is this:

   

I have the two bottom buttons but I am stuck with the top canvas and not sure how to add my buttons. I am new to python and I have been trying to follow multiple tkinter guides online and have been looking around different forums but I can't seem to make them work with what I am trying to achieve.

This is my code:
import tkinter as tk
from tkinter import scrolledtext

class GUI(object):
    
    def __init__(self, root):
        
        self.root = root
        self.root.title('Sensors')
        
        windowWidth = 700
        windowHeight = 500
        
        # get the screen dimension
        screenWidth = root.winfo_screenwidth()   # Current monitor is 1920
        screenHeight = root.winfo_screenheight() # x 1080

        # find the center point
        centerX = int(screenWidth/2 - windowWidth / 2)
        centerY = int(screenHeight/2 - windowHeight / 2)

        # set the position of the window to the center of the screen
        root.geometry(f'{windowWidth}x{windowHeight}+{centerX}+{centerY}')
        root.resizable(False,False)
        
        ### On main menu
        self.sensors = tk.Button(root, text='Sensors',height=3, width=10, font=('Helvetica',15), borderwidth=5, command=self.Sensors)
        self.options = tk.Button(root, text='Options',height=3, width=10, font=('Helvetica',15), borderwidth=5, command=self.Options)
        
        
        ### In sensor menu
        self.addSensorBtn = tk.Button(root, text='Add Sensor')
        self.ReturnMenu = tk.Button(root, text="Return to Main Menu", font=('Helvetica'), command=self.MainMenu)
       
        self.frame = tk.Frame(root).grid(row=0,column=0)
        self.canvas = tk.Canvas(self.frame,bg='gray75')
        self.vsb = tk.Scrollbar(self.frame, orient='vertical', command=self.canvas.yview)
        
        self.MainMenu()
        
    def GridConfig(self):
        root.rowconfigure(0,weight=3)
        root.rowconfigure(1,weight=1)
        root.columnconfigure((0,2), weight=1)
        
    def MainMenu(self):
         self.GridConfig()
         self.RemoveAll()
         self.sensors.grid(column=0, row=1, sticky='NE')
         self.options.grid(column=2, row=1, sticky='NW')
         
    def Sensors(self) :
        self.RemoveAll()
        
        self.canvas.grid(column=0,row=0,columnspan=3,sticky='NEWS')
        self.vsb.grid(row=0, column=3, sticky='NSE')
        self.canvas.configure(yscrollcommand=self.vsb.set, scrollregion=self.canvas.bbox('all'))
        
        self.addSensorBtn.grid(column=2,row=1, sticky='NEWS')        
        self.ReturnMenu.grid(column=0, row=1,sticky='NEWS')
    
    def Options(self):
        self.RemoveAll()
        self.GridConfig()
        self.ReturnMenu.grid(column=1, row=1)
    
    def RemoveAll(self):
        self.vsb.grid_remove()
        self.canvas.grid_remove()
        self.addSensorBtn.grid_remove()
        self.sensors.grid_remove()
        self.options.grid_remove()
        self.ReturnMenu.grid_remove()
        
if __name__ == '__main__':
    root = tk.Tk()
    myGUI = GUI(root)
    root.mainloop()
Reply


Messages In This Thread
Scrollable buttons with an add/delete button - by Clich3 - Jun-14-2022, 03:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] PyQt5 drawing on scrollable area HeinKurz 3 1,420 Mar-28-2023, 12:58 PM
Last Post: Axel_Erfurt
  [Tkinter] How to create scrollable frame mandiatutti 7 4,697 Aug-07-2021, 03:34 PM
Last Post: deanhystad
Question [Tkinter] Scrollable Treeview: change behavior of Prior/Next keys? RockDoctor 2 3,318 Apr-10-2021, 05:40 PM
Last Post: RockDoctor
  Scrollable big image in a window (TKinter) Prospekteur 3 4,611 Sep-14-2020, 03:06 AM
Last Post: Larz60+
  [Tkinter] Command button, then more command buttons Heyjoe 4 3,001 Aug-08-2020, 11:28 AM
Last Post: Yoriz
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,112 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Fixate graphs on scrollable canvas janema 6 5,548 Apr-12-2019, 03:57 PM
Last Post: Larz60+
  [Tkinter] How to get & delete details from each input by clicking a button Vicolas 6 3,976 Feb-01-2019, 11:00 AM
Last Post: Vicolas
  Buttons not appearing, first time button making admiral_hawk 5 3,498 Dec-31-2018, 03:26 AM
Last Post: admiral_hawk
  [Tkinter] ListBox not contained or scrollable kellykimble 6 5,305 Apr-05-2018, 11:59 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020