【问题标题】:how to add a vertical scrollbar to checkbox in python tkinter?如何在python tkinter中向复选框添加垂直滚动条?
【发布时间】:2020-11-26 08:57:35
【问题描述】:

我从一个文本文件中读取了复选框的输入,这已经超过了给定的窗口大小。 如何为此添加滚动条以查看复选框的所有内容。 提前致谢。

编辑:添加代码。

from tkinter import *
import os


error = []
window = ""
with open("op.txt") as inp:
    for lines in inp:
        if lines.strip() == "done":
            error.append(window)
            window = ""
        else:
            window += lines

print(len(error))

root = Tk()
root.minsize(500, 500)
cbTexts={}
cbVariables={}
cb={}
for i in error:
    cbTexts[i] = StringVar()
    cbTexts[i].set(i)
    cbVariables[i] = IntVar()
    cbVariables[i].set(0)
    cb[i] = Checkbutton(root, textvariable=cbTexts[i], variable=cbVariables[i])
    cb[i].pack()
mainloop()

【问题讨论】:

  • 请发布您的代码。

标签: python user-interface tkinter scrollbar


【解决方案1】:

您可以使用ScrolledText 来按住复选按钮。

下面是一个简单的例子:

import tkinter as tk
from tkinter.scrolledtext import ScrolledText

root = tk.Tk()

text = ScrolledText(root, width=20, height=10)
text.pack()

for i in range(30):
    cb = tk.Checkbutton(text, text=(i+1), bg='white', anchor='w')
    text.window_create('end', window=cb)
    text.insert('end', '\n')

root.mainloop()

【讨论】:

  • 另外,如果您不想误删任何复选按钮,请在添加所有复选按钮后更改text['state'] = 'disabled'
猜你喜欢
  • 2017-03-24
  • 2021-04-25
  • 2011-11-17
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多