文本小部件的滚动条问题 [英] Trouble With Scrollbar For Text Widget

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

问题描述

我只是想为出现在我的文本小部件上的复选按钮列表制作一个滚动条.到目前为止,滚动条出现在更正位置和正确的大小,但它是灰色的,不允许我滚动.有谁知道可能是什么问题?

I am simply trying to make a scrollbar for the list of checkbuttons that appear on my text widget. So far the scrollbar appears in the correction location and the correct size but it is greyed out and will not allow me to scroll. Does anyone known what the problem might be?

import tkinter
from tkinter import *

master = tkinter.Tk()
master.geometry("750x500")

checkbox_frame = Frame(master, borderwidth=1, highlightthickness=1,
                                 highlightbackground="black", highlightcolor="black")
checkbox_frame.pack(expand=False,ipadx=100,ipady=100)

text = Text(checkbox_frame, cursor="arrow")

vsb = Scrollbar(text, orient = tkinter.VERTICAL)
vsb.config(command=text.yview)

text.config(yscrollcommand=vsb.set)

text.configure(state="disabled")

vsb.pack(side=RIGHT, fill=Y)
text.pack(side="left", fill="both", expand=True)

listbox = Listbox(master)
listbox.place(x=3,y=0)
enable = []
for x_number_of_items in range(15):
    enable.append("Robot Test File "+ str(x_number_of_items))
list_for_listbox = ["one", "two", "three", "four"]

for item in list_for_listbox:
    listbox.insert(END, item)
    for y in enable:
        globals()["var{}{}".format(item, y)] = BooleanVar()
        globals()["checkbox{}{}".format(item, y)] = Checkbutton(text, text=y, variable=globals()["var{}{}".format(item, y)])

def onselect(evt):
    # Note here that Tkinter passes an event object to onselect()
    w = evt.widget
    x=0
    index = int(w.curselection()[0])
    value = w.get(index)
    print ('You selected item %d: "%s"' % (index, value))

    for y in enable:
        for item in list_for_listbox:
            globals()["checkbox{}{}".format(item, y)].place_forget()
        globals()["checkbox{}{}".format(value, y)].place(x=0,y=0+x)
        x+=50

listbox.bind('<<ListboxSelect>>', onselect)

def printcommand():
    for item in list_for_listbox:
        for y in enable:
            print(item + " [" + y + "] " + str(globals()["var{}{}".format(item, y)].get()))

print(enable)

printbutton = Button(master,text="Print",command= printcommand)
printbutton.place(x=100, y=250)

mainloop()

推荐答案

您的代码存在一些问题.

There are a couple things wrong with your code.

我要改变的第一件事是停止尝试动态生成变量名称.这实际上从来都不是一个好主意,只会使您的代码极难理解和调试.

The first thing I would change is to stop trying to dynamically generate variable names. That is virtually never a good idea, and only serves to make your code extremely hard to understand and debug.

相反,将复选按钮和变量存储在列表或字典中.不要求每个复选按钮或变量都具有与其关联的不同变量.

Instead, store the checkbuttons and variables in a list or dictionary. There's no requirement that each checkbutton or variable have a distinct variable associated with it.

例如,以下代码说明了如何在循环中创建一堆复选按钮和变量:

For example, the following code illustrates how to create a bunch of checkbuttons and variables in a loop:

for i in range(15):
    label = "Robot Test File {}".format(i)
    var = tk.BooleanVar()
    checkbutton = tk.Checkbutton(text, text=label, variable=var)
    cb_vars.append(var)
    checkbuttons.append(checkbutton)

这样,您现在可以像使用任何列表一样引用复选按钮及其变量:cb_vars[0].get() 等.如果您希望能够引用它们按名称,您可以使用字典而不是列表:

With that, you can now reference the checkbuttons and their variables as you would with any list: cb_vars[0].get(), etc. If you prefer to be able to refer to them by name then you can use a dictionary instead of a list:

cb_vars = {}
checkbuttons = []
for i in range(15):
    label = "Robot Test File {}".format(i)
    var = tk.BooleanVar()
    checkbutton = tk.Checkbutton(text, text=label, variable=var)
    cb_vars[label] = var
    checkbuttons.append(checkbutton)

    text.window_create("insert", window=checkbutton)
    text.insert("end", "\n")

有了上面的,你可以做 cb_vars['Robot Test File 1'].get() 来获取 checkbutton number 1 的值.当然你可以使用任何你想要的作为索引.

With the above, you can do cb_vars['Robot Test File 1'].get() to get the value of checkbutton number 1. You can of course use anything you want as the index.

其次,文本小部件不能滚动使用packplacegrid 添加到小部件中的内容.小部件只能滚动作为内容添加到小部件的内容.要添加复选按钮,您可以使用 window_create 方法.假设您希望每个复选按钮位于单独的行上,则需要在每个复选按钮后添加一个换行符.

Second, the text widget can't scroll things added to the widget with pack, place, or grid. The widget can only scroll things added as content to the widget. To add a checkbutton you can use the window_create method. Assuming you want each checkbutton on a separate line, you'll need to add a newline after each checkbutton.

例如,以下是在可滚动文本小部件中创建 15 个复选框的方法:

For example, here's how you could create 15 checkboxes in a scrollable text widget:

import tkinter as tk

root = tk.Tk()

text = tk.Text(root, height=5)
vsb = tk.Scrollbar(root, orient="vertical", command=text.yview)
text.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
text.pack(side="left", fill="both", expand=True)

cb_vars = []
checkbuttons = []
for i in range(15):
    label = "Robot Test File {}".format(i)
    var = tk.BooleanVar()
    checkbutton = tk.Checkbutton(text, text=label, variable=var)
    var.set(False)

    text.window_create("insert", window=checkbutton)
    text.insert("end", "\n")
    cb_vars.append(var)
    checkbuttons.append(checkbutton)

root.mainloop()

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

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