i got a little problem with scrollbars in tkinter

Post here if you need help with creating a Graphical User Interface in Python.

i got a little problem with scrollbars in tkinter

Postby xzenon » Tue Sep 06, 2016 9:33 am

i got a little problem with my scrollbar and listbox. the problem is i cant put them near each other. if i put them in two separate columns they end up with a big space between them and when i put them in the same column the scrollbar ends up covering the right part of the listbox. Can any one help me out with this pls?

Code: Select all
from tkinter import *
from tkinter import ttk
#öppna data filer
file1 = open('partmärkning.txt', 'wt')
file2 = open('kabelmärking.txt', 'wt')

#funktioner
def make(*args):
    try:
        a = kabelsort.get()
        if (a == 1):
               value1 = (kabel.get())
               value2 = (part.get())
               for parts in range(value2):
                   file1.writelines('{}.{}\n'.format(value1, parts+1))
               file1.writelines('{}.{}\n'.format(value1, "SC"))   
               file2.writelines('{}\n'.format(value1))
               list.insert(END, value1)
               print(value1)

        if (a == 2):
               value1 = (kabel.get())
               value2 = (part.get())
               for parts in range(value2):
                   file1.writelines('{}.{}\n'.format(value1, parts+1))
               file2.writelines('{}\n'.format(value1))
               print(value1)

        if (a == 3):
               value1 = (kabel.get())
               value2 = (part.get())
               if (value2 == 3):
                   file1.writelines('{}.{}\n'.format(value1, "BN"))
                   file1.writelines('{}.{}\n'.format(value1, "BU"))
                   file1.writelines('{}.{}\n'.format(value1, "YE/GN"))
                   file1.writelines('{}.{}\n'.format(value1, "SC"))
                   file2.writelines('{}\n'.format(value1))
                   print(value1)                   
               elif (value2 == 5):
                   file1.writelines('{}.{}\n'.format(value1, "BN"))
                   file1.writelines('{}.{}\n'.format(value1, "BK"))
                   file1.writelines('{}.{}\n'.format(value1, "GY"))
                   file1.writelines('{}.{}\n'.format(value1, "BU"))
                   file1.writelines('{}.{}\n'.format(value1, "YE/GN"))
                   file1.writelines('{}.{}\n'.format(value1, "SC"))   
                   file2.writelines('{}\n'.format(value1))
                   print(value1)
    except ValueError:
        pass

root = Tk()
root.title("kabelmärkarn")

#fönster designern
mainframe = ttk.Frame(root, padding="4 4 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

#data strings som används av funktioner
kabel = StringVar()
part = IntVar()
kabelsort = IntVar()
kabelsort.set(1)

#fönster designern. här bestäms vad du ska ha och vart det ska placeras etc

#etikettera, textrutor
ttk.Label(mainframe, text="Kabelnamn:").grid(column=2, row=0, sticky=(W, E), columnspan=2)
kabelnamn_entry = ttk.Entry(mainframe, textvariable=kabel).grid(column=2, row=1, sticky=(W, E), columnspan=2)

ttk.Label(mainframe, text="Parter:").grid(column=2, row=2, sticky=(W, E), columnspan=2)
part_entry = ttk.Entry(mainframe, textvariable=part).grid(column=2, row=3, sticky=(W, E), columnspan=2)

#radioknappar
sort1 = ttk.Radiobutton(mainframe, text='Skärmad kabel', variable=kabelsort, value='1').grid(column=2, row=4)
sort2 = ttk.Radiobutton(mainframe, text='Oskärmad', variable=kabelsort, value='2').grid(column=3, row=4)
sort3 = ttk.Radiobutton(mainframe, text='Eklk/Eqlq färged', variable=kabelsort, value='3').grid(column=2, row=5, sticky=W)

#Listbox
listbox = Listbox(mainframe, height=10)
listbox.grid(column=0, row=0, rowspan=6, sticky=(N, W, S))

# create a vertical scrollbar to the right of the listbox
s = ttk.Scrollbar(mainframe, orient=VERTICAL, command=listbox.yview)
s.grid(column=1, row=0, rowspan=6, sticky=(N, E, S))
listbox['yscrollcommand'] = s.set
#ttk.Sizegrip().grid(column=1, row=1, sticky=(S,E))


#knapp
ttk.Button(mainframe, text="Make", command=make).grid(column=3, row=5,sticky=E)


for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

root.mainloop()

#stänger ner data filer och sparar data
file1.close()
file2.close()
Last edited by micseydel on Tue Sep 06, 2016 2:37 pm, edited 1 time in total.
Reason: Moved to GUI.
xzenon
 
Posts: 12
Joined: Sun Mar 01, 2015 4:52 pm

Re: i got a little problem with scrollbars in tkinter

Postby Larz60+ » Fri Sep 09, 2016 10:21 am

Hello,

A widget doesn't actually allow for more than one vertical and one horizontal scrollbar.
It will try, but mess up. Why would you need more than one? Did you want to add a horizontal bar?

I took the liberty to modify your software to use a class, and added some dummy data to the listbox

Code: Select all
from tkinter import *
import tkinter.ttk as ttk


class MyApp:
    def __init__(self, parent):
        self.parent = parent
        self.öppna_data_filer()

    #funktioner
    def make(self, *args):
        try:
            a = kabelsort.get()
            if (a == 1):
                   value1 = (kabel.get())
                   value2 = (part.get())
                   for parts in range(value2):
                       file1.writelines('{}.{}\n'.format(value1, parts+1))
                   file1.writelines('{}.{}\n'.format(value1, "SC"))
                   file2.writelines('{}\n'.format(value1))
                   list.insert(END, value1)
                   print(value1)

            if (a == 2):
                   value1 = (kabel.get())
                   value2 = (part.get())
                   for parts in range(value2):
                       file1.writelines('{}.{}\n'.format(value1, parts+1))
                   file2.writelines('{}\n'.format(value1))
                   print(value1)

            if (a == 3):
                   value1 = (kabel.get())
                   value2 = (part.get())
                   if (value2 == 3):
                       file1.writelines('{}.{}\n'.format(value1, "BN"))
                       file1.writelines('{}.{}\n'.format(value1, "BU"))
                       file1.writelines('{}.{}\n'.format(value1, "YE/GN"))
                       file1.writelines('{}.{}\n'.format(value1, "SC"))
                       file2.writelines('{}\n'.format(value1))
                       print(value1)
                   elif (value2 == 5):
                       file1.writelines('{}.{}\n'.format(value1, "BN"))
                       file1.writelines('{}.{}\n'.format(value1, "BK"))
                       file1.writelines('{}.{}\n'.format(value1, "GY"))
                       file1.writelines('{}.{}\n'.format(value1, "BU"))
                       file1.writelines('{}.{}\n'.format(value1, "YE/GN"))
                       file1.writelines('{}.{}\n'.format(value1, "SC"))
                       file2.writelines('{}\n'.format(value1))
                       print(value1)
        except ValueError:
            pass

    def öppna_data_filer(self):
        #öppna data filer (open data files)
        file1 = open('partmärkning.txt', 'wt')
        file2 = open('kabelmärking.txt', 'wt')

    def main(self):
        self.parent.title("kabelmärkarn")

        # fönster designern (The designer window)
        # mainframe = ttk.Frame(self.parent, padding="4 4 12 12")
        mainframe = ttk.Frame(self.parent, padding="4 4 12 12")
        mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
        mainframe.columnconfigure(0, weight=1)
        mainframe.rowconfigure(0, weight=1)

        # data strings som används av funktioner (data strings used by functions)
        kabel = StringVar()
        part = IntVar()
        kabelsort = IntVar()
        kabelsort.set(1)

        # fönster designern. här bestäms vad du ska ha och vart det ska placeras etc
        # (window designer. This is determined what to wear and where it should be placed, etc.)

        # etikettera, textrutor (label, text boxes)
        ttk.Label(mainframe, text="Kabelnamn:").grid(column=2, row=0, sticky=(W, E), columnspan=2)
        kabelnamn_entry = ttk.Entry(mainframe, textvariable=kabel).grid(column=2, row=1, sticky=(W, E), columnspan=2)

        ttk.Label(mainframe, text="Parter:").grid(column=2, row=2, sticky=(W, E), columnspan=2)
        part_entry = ttk.Entry(mainframe, textvariable=part).grid(column=2, row=3, sticky=(W, E), columnspan=2)

        # radioknappar (radio buttons)
        sort1 = ttk.Radiobutton(mainframe, text='Skärmad kabel', variable=kabelsort, value='1').grid(column=2, row=4)
        sort2 = ttk.Radiobutton(mainframe, text='Oskärmad', variable=kabelsort, value='2').grid(column=3, row=4)
        sort3 = ttk.Radiobutton(mainframe, text='Eklk/Eqlq färged', variable=kabelsort, value='3').grid(column=2, row=5,
                                                                                                        sticky=W)

        # Listbox
        listbox = Listbox(mainframe, height=10)
        listbox.grid(column=0, row=0, rowspan=5, sticky=(N, W, S))

        # Lägg till några data till listruta
        for x in range(20):
            listbox.insert(x, str(x))

        # create a vertical scrollbar to the right of the listbox
        # förändring från - Att förmodligen inte nödvändigt (Change from)
        scrollbar1 = ttk.Scrollbar(mainframe, orient=VERTICAL)
        listbox.config(yscrollcommand=scrollbar1.set)
        scrollbar1.config(command=listbox.yview)
        scrollbar1.grid(column=1, row=0, rowspan=5, sticky=N + S + E)
        # ttk.Sizegrip().grid(column=1, row=1, sticky=(S,E))


        #  (Button)
        ttk.Button(mainframe, text="Make", command=self.make).grid(column=3, row=5, sticky=E)

        for child in mainframe.winfo_children():
            child.grid_configure(padx=5, pady=5)

    def stänga_filer(self):
        # stänger ner data filer och sparar data (shut down the computer files and saves data)
        file1.close()
        file2.close()


if __name__ == '__main__':
    root = Tk()
    app = MyApp(root)
    app.main()
    root.mainloop()


You'll need to add a quit button to call stänga_filer.

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: i got a little problem with scrollbars in tkinter

Postby xzenon » Fri Sep 09, 2016 4:47 pm

Hi tnx for the answer. But it dident solve my problem with the scrollbar and listbox being to far from eachother . But Nvm this topic because i solved it a couple of Days ago. This is my first Tkinter based program and probably serious python program. I know its maybe a stupid question but could you explain why i should use a class and not do it like i have?
xzenon
 
Posts: 12
Joined: Sun Mar 01, 2015 4:52 pm

Re: i got a little problem with scrollbars in tkinter

Postby Larz60+ » Fri Sep 09, 2016 7:28 pm

Hello,

Nothing says that you have to use classes.
It containerizes code, and a class should relate to just one subject.
I always use them as it makes reading, and debugging much easier, and most importantly,
classes can be put into separate files, and if properly written re-used over and over as 'Black boxes'
by simply importing them.

If you want the scrollbar touching, use:
Code: Select all
scrollbar1.grid(column=0, row=0, rowspan=5, sticky=N + S + E)


changed column to zero (code I submitted last post)

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: i got a little problem with scrollbars in tkinter

Postby xzenon » Fri Sep 09, 2016 9:13 pm

Hi
Thank you for the explanation. It sound very logical to do it like you explained.
xzenon
 
Posts: 12
Joined: Sun Mar 01, 2015 4:52 pm

Re: i got a little problem with scrollbars in tkinter

Postby Larz60+ » Sat Sep 10, 2016 11:50 pm

Hello,

You might also want to take a look at pmw (written entirely in python and tkinter).

See my post http://www.python-forum.org/viewtopic.php?f=10&t=20968.

The scrolled frame takes care of the scrollbars for you.
The example includes proper resizing.

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm


Return to GUI

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests