问题
I am writing a single thread program with a GUI, which executes a series of tasks. During these tasks the GUI is refreshed regularly to check for a few available inputs (e.g. abort). To avoid halting the task with unwanted inputs all unnecessary GUI elements are disabled by .config(state='disabled')
during execution.
This however does not seem work for scrollbars which for some reason are unique and don't have a "-state"
option.
回答1:
I think you can't disable the scrollbar widget using the -state
option, I've searched in the Tcl/Tk website and I didn't find anything about it.
But I am sure you can use the pack_forget() method to delete it from the window if you are using the pack geometry or the grid_forget() if you are using the grid geometry, so you can later show the scrollbar widget again calling pack/grid
again.
Here's a example:
from tkinter import *
master = Tk()
scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT, fill=Y)
def disable_scroll():
scrollbar.pack_forget()
def active_scroll():
scrollbar.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=None)
btn1 = Button(master, text="OK 1", command=disable_scroll)
btn1.pack()
btn2 = Button(master, text="OK 2", command=active_scroll)
btn2.pack()
listbox = Listbox(master, yscrollcommand=scrollbar.set)
for i in range(1000):
listbox.insert(END, str(i))
listbox.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=listbox.yview)
mainloop()
The Scrollbar changes according the Listbox's values, so as I said here's the code:
from tkinter import *
import random
master = Tk()
scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT, fill=Y)
def disable_scroll():
scrollbar.pack_forget()
def active_scroll():
scrollbar.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=None)
btn1 = Button(master, text="disable_scroll", command=disable_scroll)
btn1.pack()
btn2 = Button(master, text="active_scroll", command=active_scroll)
btn2.pack()
listbox = Listbox(master, yscrollcommand=scrollbar.set)
#NEW FUNCTIONS
def count():
listbox.delete(0, END)
lista = [5, 8, 500, 1000]
for i in range(random.choice(lista)):
listbox.insert(END, str(i))
def delete():
listbox.delete(0, END)
#NEW BUTTONS
btn3 = Button(master, text="start", command=count)
btn3.pack()
btn4 = Button(master, text="delete", command=delete)
btn4.pack()
listbox.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=listbox.yview)
mainloop()
回答2:
Another possible approach to get the scroll bar to display with a disabled look (instead of being hidden) is to:
- Remove all entries in the list (the scroll bar will become disabled)
- Detach the scroll bar from the list
- Re-insert the removed entries with the scroll bar detached (the scroll bar will remain disabled)
In the code below, 'listbox' is my tk.Listbox object,'scrollbar' is the associated 'tk.Scrollbar' object, and self is a reference to the parent object. Code tested on Window with Python 3.8.4rc1.
Disable code:
entries = listbox.get(0, "end")
listbox.delete(0, "end")
self.update()
listbox.config(yscrollcommand="")
scrollbar.config(command="")
listbox.insert("end", *entries)
listbox['state'] = "disabled"
Enable code:
listbox['state'] = "normal"
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
来源:https://stackoverflow.com/questions/57499836/how-to-disable-a-tkinter-scrollbar-widget