如何禁用 TKinter 滚动条小部件? [英] How to disable a TKinter scrollbar widget?

查看:34
本文介绍了如何禁用 TKinter 滚动条小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个带有 GUI 的单线程程序,它执行一系列任务.在这些任务期间,GUI 会定期刷新以检查一些可用的输入(例如中止).为了避免因不需要的输入而停止任务,所有不必要的 GUI 元素在执行过程中都被 .config(state='disabled') 禁用.然而,这似乎不适用于由于某种原因是唯一的并且没有 "-state" 选项的滚动条.

解决方案

我认为您无法使用 -state 选项禁用滚动条小部件,我已在 Tcl/Tk 网站,我没有找到任何相关信息.>

但我相信你可以使用 pack_forget() 如果您使用的是 pack 几何图形或 grid_forget() 如果您使用的是 grid 几何图形,以便您稍后可以再次调用 pack/grid 再次显示滚动条小部件.

举个例子:

from tkinter import *主 = Tk()滚动条 = 滚动条(主)scrollbar.pack(side=RIGHT, fill=Y)def disable_scroll():scrollbar.pack_forget()def active_scroll():scrollbar.pack(side=LEFT, fill=BOTH)scrollbar.config(命令=无)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)对于我在范围内(1000):listbox.insert(END, str(i))listbox.pack(side=LEFT, fill=BOTH)scrollbar.config(命令=listbox.yview)主循环()

滚动条根据列表框的值而变化,正如我所说的代码:

from tkinter import *随机导入主 = Tk()滚动条 = 滚动条(主)scrollbar.pack(side=RIGHT, fill=Y)def disable_scroll():scrollbar.pack_forget()def active_scroll():scrollbar.pack(side=LEFT, fill=BOTH)scrollbar.config(命令=无)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)#新功能定义计数():listbox.delete(0, END)列表 = [5, 8, 500, 1000]对于我在范围内(random.choice(lista)):listbox.insert(END, str(i))定义删除():listbox.delete(0, END)#新按钮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(命令=listbox.yview)主循环()

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.

解决方案

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()

这篇关于如何禁用 TKinter 滚动条小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆