Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tkinter Scrollbar makes button disappear

I just wanted to add a scrollbar into my code but it destroys my button.

import random
import string
from tkinter import *
from tkinter import ttk

def passcreate():
    passlen = 16
    pass_chars = "AaBbCcDd" #string.ascii_letters + string.digits
    passw = ''.join(random.choice(pass_chars) for i in range(passlen))
    liste.insert(END, passw)

def donothing():
   filewin = Toplevel(root)
   button = Button(filewin, text="Do nothing button")
   button.pack()
 
root = Tk()

menubar = Menu(root)
file_menu = Menu(menubar, tearoff=0)

menubar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Edit", command=donothing)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)

pw = PanedWindow(orient="vertical")

buton = ttk.Button(pw, text= "BUTTON", command=passcreate)
buton.pack(side = TOP)

liste = Listbox(pw)
liste.pack(side=TOP)

pw.add(liste)
pw.add(buton)
pw.pack(fill = BOTH, expand = True)
pw.configure(sashrelief = RAISED)

root.config(menu=menubar)

root.mainloop()

In this code there is no problem (i think so)
but when i want to add this part, my button just disappears

scrollbar = Scrollbar(root)
liste.config(yscrollcommand= scrollbar.set)
liste.pack(side= "right", fill= Y)
scrollbar.config(command= liste.yview)

And I want the code to look like without the scrollbar part

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Generally I would not recommend to import a package with * This fills up your namespace which can lead to conflicts. So better import tkiner as tk

This code should work for you

pw = PanedWindow(orient="vertical")

buton = ttk.Button(root, text="BUTTON", command=passcreate)
buton.pack(side=BOTTOM)

scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)

liste = Listbox(pw, yscrollcommand=scrollbar.set)
liste.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=liste.yview)

pw.pack(fill=BOTH, expand=True)
pw.configure(sashrelief=RAISED)

root.config(menu=menubar)

root.mainloop()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading