Python Forum
[Tkinter] Scroll Bars going backwards - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Scroll Bars going backwards (/thread-18934.html)



Scroll Bars going backwards - goofygoo - Jun-07-2019

I am having difficulty with my scroll bars sliding backward. Code below.

user_dict = {"J Schmo":["SSM", "CPP"], "B Kezzi":["SM", "OBSIC"], "J juk":["SSM", "CPP"]}
system_type = {"Controller":["SSM", "CPP", "SM"], "CTD":["SSM", "SM"], "MMP":["SSM", "CPP"], "Acomm":["SSM"], "D2": ["OBSIC"]}
user_variables = {"000System SN": ["CTD", "SSM", "SM", "OBSIC"], "001System IP": ["CTD", "MMP", "SSM", "SM", "OBSIC"], "002System Name":
 ["MMP", "SSM", "SM", "OBSIC"]}
system_variables = {"000Initial Main V": ["CTD", "SSM", "SM", "OBSIC"],"001Initial Main I": ["CTD", "SSM", "SM", "OBSIC", "D2"]}
user_buttons = {"LOGIN": ["SSM", "CPP", "SM"], "Get Address": ["SSM", "CPP"], "Connect": ["OBSIC"], "Begin Test": ["OBSIC"], "Get initial Q330 V&I": ["OBSIC", "D2"]}

users = list(user_dict.keys())  # Grabs all users from user_dict and put them in a list called users
User_Variable_Entry = {}
User_Variable_Label = {}
System_Variable_Entry = {}
System_Variable_Label = {}

def in_group(dictionary, group):
    temp = []
    for key, groups in dictionary.items():
        if group in groups:
            temp.append(key)
    return temp

def system_type_menu(selection):
     global system
     system = selection
     systems = in_group(system_type, selection)
     Userlist = OptionMenu (frm, system_type_var, *systems, command = test_data) # Step Three
     Userlist.grid(row=2, column=0,pady=10)

def group_menu(selection):
    groups = user_dict[selection]
    Userlist = OptionMenu (frm, group_var, *groups, command = system_type_menu) # Step Two
    Userlist.grid(row=1, column=0,padx=5)

def test_data(selection): # Step Four
   UVP = 20 ##User Variable Position
   lb = list()
   for k, v in sorted(user_variables.items()):
      if system in v:
         name = (k)
         e = Entry(frm, width=25)
         e.grid(sticky=W,row=UVP, column=3)
         User_Variable_Entry[name] = e
         lb = Label(frm, width=25, text=name[3:])
         lb.grid(row=UVP, column=15)
         User_Variable_Label[name] = lb
         UVP += 30

   BPH = 420
   BPV = 525
   x=0
   for k, v in sorted(user_buttons.items()):
      if system in v:
         BV = k
         if (x == 3):
            x=0
            BPV += 30
            BPH = 420
         BV = Button(frm, width=20, bg = "green", fg = "white", text=BV)
         BV.grid(row=BPH, column=BPV)
         BPH += 200
         x += 1

   SVP = 50 ##System Variable Position
   lb = list()
   for k, v in sorted(system_variables.items()):
      if system in v:
         name = (k)
         e1 = Entry(frm, width=25)
         e1.grid(row=1275, column=SVP)
         System_Variable_Entry[name] = e1
         lb1 = Label(frm, width=25, text=name[3:])
         lb1.grid(row=1050, column=SVP)
         System_Variable_Label[name] = lb1
         SVP += 30

def get_data():
    for k in sorted(user_variables.keys()):
      print(k.get())

def print_user_variables():
    for name in sorted(User_Variable_Entry):
        print ((name[3:]),User_Variable_Entry[name].get())


from tkinter import *
## Main window
root = Tk()
## Grid sizing behavior in window
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
## Canvas
cnv = Canvas(root)
cnv.grid(row=0, column=0, sticky='nswe')
## Scrollbars for canvas
hScroll = Scrollbar(root, orient=HORIZONTAL, command=cnv.xview)
hScroll.grid(row=1, column=0, sticky='we')
vScroll = Scrollbar(root, orient=VERTICAL, command=cnv.yview)
vScroll.grid(row=0, column=1, sticky='ns')
cnv.configure(xscrollcommand=hScroll.set, yscrollcommand=vScroll.set)

## Frame in canvas
frm = Frame(cnv, bg='cyan')
## This puts the frame in the canvas's scrollable zone
cnv.create_window(0, 0, window=frm, anchor='nw')
## Frame contents

a = StringVar()

title_label = Label(frm, text="My Software}",font=(None, 36),bg='cyan',fg = "blue")
title_label.place(x=20, y=0)

Run_now = Button(frm, text="Run", fg="blue", command=print_user_variables)
Run_now.place(x=500, y=20)

Exit_now = Button(frm, text="Quit", fg="blue", command=root.destroy)
Exit_now.place(x=500, y=40)

UV_Label = Label(frm, bg="cyan", fg="blue", text="USER VARIABLES",font=(None, 18))
UV_Label.place(x=10, y=60)

SV_Label = Label(frm, width=25,bg='cyan',fg = "blue", text="SYSTEM VARIABLES",font=(None, 18))
SV_Label.place(x=10, y=80)

user_var = StringVar()
user_var.set ('Enter details')

system_type_var = StringVar()
system_type_var.set ('Select System Type')

group_var = StringVar()
group_var.set ('Select Group')

var=StringVar()
var.set ('Select User')
Userlist = OptionMenu (frm, var, *users, command = group_menu) # Step One Changed *list to *users, added command = group_menu
Userlist.grid(row=0, column=0)


## Update display to get correct dimensions
frm.update_idletasks()
## Configure size of canvas's scrollable zone
cnv.configure(scrollregion=(0, 0, frm.winfo_width(), frm.winfo_height()))

root.mainloop()



RE: Scroll Bars going backwards - Larz60+ - Jun-07-2019

You are trying to scroll the root window, which can't be scrolled.
Also, the hierarchy of your windows isn't right
should be:
frame in root (you have this right)
canvas in frame (you have it attached to root)
scrollbars attached to canvas, but linked to frame. It's hard to explain what i mean here.
you can find an example that you can follow in the following code: https://github.com/Larz60p/CaliforniaPublicSalaries/blob/master/src/CaCompGui.py


RE: Scroll Bars going backwards - goofygoo - Jun-07-2019

Thank you for the response, I don't see where I have the frame attached to root
canvas in frame (you have it attached to root)