0

I've seen multiple other answers to this question, which includes solutions such as adding Canvas and Frames, but every time I try to do that, the checkbuttons do not display in my window.

This code is basically to display a window with chrome history listed with checkbuttons and allow the user to select what they want to delete. When they scroll down and click the Delete button, the history is cleared.

I've managed to make everything except for the scrollbar work.

import sqlite3
import os
from tkinter import *

path = os.path.expanduser('~/AppData/Local/Google/Chrome/User Data/Default/History')
conn = sqlite3.connect(path)
cur = conn.cursor()
select_statement = "SELECT urls.url FROM urls, visits WHERE urls.id = visits.url;"
cur.execute(select_statement)
results = cur.fetchall()

root = Tk()
text = root

# window title
title = Label(root,text = "Check the websites you want to delete : \n", justify = LEFT)
title.grid(row=1,column=0)

# Removing the files from the sql database
ids=[]
def delHis():
    global cbs
    for name, checkbutton in cbs.items():
        if checkbutton.var.get():
            for rows in cur.execute('''SELECT urls.id,urls.url FROM urls,visits WHERE urls.id = visits.url AND urls.url = (?); ''', [checkbutton['text']]):
                print(rows)
                id = rows[0]
                ids.append((id,))
            cur.executemany('''DELETE FROM visits WHERE id = ?''', ids)
            cur.executemany('''DELETE FROM urls WHERE id = ?''', ids)
            conn.commit()

# Creating the checkbuttons
cbs = dict()
for i,value in enumerate(results):
    cbs[value] = Checkbutton(text, text = value, onvalue=True,offvalue=False, wraplength=500, )
    cbs[value].var = BooleanVar(text, value=False)
    cbs[value]['variable'] = cbs[value].var
    cbs[value].grid(row=i+3, column=0)

# delete button that links to delHis()
button = Button(text, text='Delete')
button.config(height = 2, width = 50, bg = '#ff6666', fg = '#ffffff', command=delHis)

try:
    button.grid(row=i+8, column=0)
except:
    title = Label(text, text="History is empty", justify=LEFT)
    title.pack()

text.grid()
root.mainloop()

I'm pretty new to all this, so please be specific. Thank you.

Lee Yerin
  • 37
  • 4
  • Does this answer your question? [Adding a scrollbar to a group of widgets](https://stackoverflow.com/a/3092341/7414759) – stovfl Jun 17 '20 at 20:45
  • @stovfl That answer uses Pack and even when I tried modifying it, as I mentioned in my question, the checkbuttons did not appear since they are on the root window. – Lee Yerin Jun 17 '20 at 20:49
  • 1
    ***since they are on the root window.***: What is stopping you to layout it on the inner `Frame` as required if you want to scroll it? – stovfl Jun 17 '20 at 20:50
  • @stovfl I'm not sure if I'm doing it right since it's my first time with tkinter. Can you explain it? Thanks. Edit: I actually ran the code with `text = root` which I have edited into the question now. (Earlier it was `text = Frame(root)` which did not display the checkbuttons) – Lee Yerin Jun 17 '20 at 20:55
  • The common way to scroll a group of widgets are shown on the given link. The simplest for you is to follow this answer. Feel free to use Grid instead of Pack. – stovfl Jun 17 '20 at 20:57
  • You simply cannot scroll a frame of widgets without using some other widget that supports scrolling, such as a canvas or text widget. Frames simply do not support scrolling. – Bryan Oakley Jun 17 '20 at 21:20
  • 1
    @stovfl I've managed to make it work! Thank you. – Lee Yerin Jun 17 '20 at 21:25
  • Does this answer your question? [Adding a scrollbar to a group of widgets in Tkinter](https://stackoverflow.com/questions/3085696/adding-a-scrollbar-to-a-group-of-widgets-in-tkinter) – Brian Tompsett - 汤莱恩 Jun 18 '20 at 12:08

0 Answers0