0

I am trying to make a program that will make python easier using OOP, and when I tried to make a horizontal scrollbar. It did not end well

This is my code for the scrollbar:

    def add_scrollbar(self, pos1, pos2, orientation, List):

        orientation = orientation.upper()

        if orientation == "HORIZONTAL":
            scrollbar = Scrollbar(self.Created_Window, orient=HORIZONTAL)
            scrollbar.pack(side=RIGHT, fill=Y)
        elif orientation == "VERTICAL":
            scrollbar = Scrollbar(self.Created_Window, orient=VERTICAL)
            scrollbar. Pack(side=RIGHT, fill=Y)

        mylist = Listbox(self.Created_Window, yscrollcommand=scrollbar.set)
        for line in range(len(List)):
            mylist.insert(END, List[line])

        mylist.place(x=pos1, y=pos2)
        scrollbar.config(command=mylist.yview)

This the main testing file code:

import My Previous code file

Ls = ["line1", "line2", "line3", "line4", "line5", "line6", "line7", "line8"]

easyF = My Previous code file.MyClass()

easyF.create_window("test", "100x100")
easyF.add_scrollbar(250, 250, "horizontal", Ls)
easyF.start_window()

This is the image for what happened

[](https://i.stack.imgur.com/3puvK.jpg)

Any ideas why?

2 Answers2

0

You're explicitly telling the scrollbar to be on the right and fill in the y direction. If it's a horizontal scrollbar, the position would be at the bottom and fill in the x direction.

scrollbar = Scrollbar(self.Created_Window, orient=HORIZONTAL)
scrollbar.pack(side=BOTTOM, fill=X)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

The scrollbar does not align properly because you put it at the right side of the window explicitly. Also .place() is used on mylist but pack() is used on scrollbar, and it is hard to align them using different layout functions.

I would suggest to put them in a frame so that they can stick together easily:

def add_scrollbar(self, pos1, pos2, orientation, List):

    orientation = orientation.upper()

    # use a frame to hold the listbox and scrollbar,
    # so they can stick together
    frame = Frame(self.Created_Window)
    frame.place(x=pos1, y=pos2)

    mylist = Listbox(frame)  # create the listbox

    if orientation == "HORIZONTAL":
        scrollbar = Scrollbar(frame, orient=HORIZONTAL, command=mylist.xview)
        scrollbar.pack(side=BOTTOM, fill=X) # put at bottom side for horizontal scrollbar
        mylist.config(xscrollcommand=scrollbar.set)
        mylist.pack(side=TOP) # put listbox above the scrollbar
    elif orientation == "VERTICAL":
        scrollbar = Scrollbar(frame, orient=VERTICAL, command=mylist.yview)
        scrollbar.pack(side=RIGHT, fill=Y) # put at right side for vertical scrollabr
        mylist.config(yscrollcommand=scrollbar.set)
        mylist.pack(side=LEFT) # put listbox at the left side
    else:
        # invalid orientation, raise exception
        raise Exception(f"Invalid orientation: {orientation}")

    for line in List:
        mylist.insert(END, line)

Result:

enter image description here

acw1668
  • 40,144
  • 5
  • 22
  • 34