【问题标题】:inserting a checkbox into a text box, python 3.6将复选框插入文本框,python 3.6
【发布时间】:2017-08-17 00:25:33
【问题描述】:

正如标题所说,我想在文本框中插入一个复选框。我已经尝试并搜索了很多我能想到的不同方式,但没有任何效果。任何帮助是极大的赞赏! 我不会列出我尝试过的所有方法,否则这个页面会非常长。

以下代码只是一个没有复选框的示例。

import tkinter as tk
self = tk.Tk()
TEXT_BOX = tk.Text(self, width = 20, height = 10)
TEXT_BOX.pack()
app = self
app.mainloop()

这也是我想使用的复选框,我只是不知道如何插入它。

var1 = tk.IntVar()
TEST = tk.Checkbutton(self, variable = var1)

如果您知道更好的方法,请告诉我。

感谢 Bryan 解决了。工作代码是;

import tkinter as tk
self = tk.Tk()
TEXT_BOX = tk.Text(self, width = 20, height = 10)
TEXT_BOX.pack()
var1 = tk.IntVar()
TEST = tk.Checkbutton(self, variable = var1)
TEXT_BOX.window_create("1.0", window = TEST)
TEXT_BOX.insert("end", "\n")
app = self
app.mainloop()

【问题讨论】:

  • 与其给出如何模拟数据的说明,不如花时间重写您的示例,这样我们就不必这样做了。另外,你说你已经尝试了很多东西,但我没有看到任何试图创建检查按钮的东西。
  • 更新了代码和语句。 l
  • -如果对您有帮助,请告诉我。在我完成之前不小心点击了添加评论按钮。
  • 没用。它仍然需要一个外部文件,并且仍然没有显示任何创建复选框的尝试。为什么不能硬编码几行数据?此外,如果您要问的只是如何将复选按钮添加到文本小部件,请考虑删除选项菜单和搜索框。请参阅以下链接了解如何创建minimal reproducible example
  • 我提供了特定代码以帮助了解我的主要代码的结构,但如果它对人们有所帮助,我可以将其全部分解为一个文本框。另外,我对 Python 还是很陌生,那么我将如何对可搜索数据进行硬编码?

标签: python python-3.x checkbox tkinter textbox


【解决方案1】:

下面显示了一个示例,说明如何创建多个CheckButtons,然后干净地检查哪些已被选中:

from tkinter import * #imports tkinter

root = Tk() #sets root as the Tk window

examples = 10 #sets the number of example to create

label = [] #empty array for storing labels
checkbox = [] #empty array for storing checkboxes
array = [] #empty array for storing checkbox boolean states

for i in range(examples): #for loop for creating the contents of the page
    array.append(BooleanVar()) #adds an empty tk booleans value to array
    label.append(Label(root, text="Example: "+str(i))) #creates a label
    label[i].grid(column=0, row=i) #grids the above label
    checkbox.append(Checkbutton(root, variable=array[i])) #creates a button. the state variable for the button is stored in the next element of the array
    checkbox[i].grid(column=1, row=i) #grids button

def command(): #command executed from button below
    for i in range(examples): #for loop for each example
        if array[i].get() == True: #checks if the button is ticked (True)
            print("Example "+str(i)+" is ticked.") #prints this if the button is ticked
        else:
            print("Example "+str(i)+" is not ticked.") #prints this if the button is not ticked

button = Button(root, text="Ok", command=command) #creates a button to process the output

button.grid() #grids button

root.mainloop() #starts event loop

这将创建一个数组,其中每个element 都与相关checkbuttonboolean 状态相关联。然后程序使用for 循环遍历数组并根据if 循环打印结果,该循环检查每个booleanboolean 状态。

类似的原则也适用于您要查找的内容,但显然不是打印无用的语句,而是对您的数据采取某种措施。

【讨论】:

  • 这是一个很好的例子,内容丰富,对我的下一部分有很大帮助,但是如何将这些复选框放入文本框中?
  • 就像在 entry 小部件中一样?
  • 是的,如果我不是很具体,很抱歉。例如,我试图将复选框放入文本字​​段。
【解决方案2】:

要将检查按钮(或任何小部件)插入文本小部件,请使用记录在案的方法 window_create

例子:

TEXT_BOX.window_create("1.0", window=TEST)
TEXT_BOX.insert("end", "<-- a checkbox!\n")

这在文本小部件文档(例如:help(tk.Text))以及许多流行教程中都有提及:

【讨论】:

  • 您的方法非常有效,我也已将这些页面添加为书签,并将仔细查看它们,谢谢!
猜你喜欢
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 1970-01-01
  • 2014-09-15
  • 2014-05-22
  • 1970-01-01
  • 2013-03-27
相关资源
最近更新 更多