Try to dynamically create a Checkbutton with Python's Tkinter

* Introduction

In Last time, I wrote the basic part of Tkinter in Python. This time, I will show you how to dynamically create a Tkinter Checkbutton.

*flow

A flow diagram of a simple idea.

  1. Create a Tkinter framework
  2. Prepare a global variable to store the result of Checkbutton (whether it is checked or not) and the handle of Checkbutton.
  3. Create a Checkbutton and store the contents of BooleanVar and the handle of Checkbutton in a global variable. that's all.

*code

Python


#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import Tkinter
import tkMessageBox


root = Tkinter.Tk()
root.title(u"Software Title")
root.geometry("400x300")


#
#Global variables
#
hLabel = []				#Stores the label handle
hCheck = []				#Stores the handle of the checkbox
CheckVal = []			#Stores whether the checkbox is checked


#
#Get the check status of a checkbox
#
def check(event):
	for n in range(len(CheckVal)):
		if CheckVal[n].get() == True:
			label = Tkinter.Label(text=u"Checked")
			label.place(x=100, y=20*n + 50)
		else:
			label = Tkinter.Label(text=u"Not checked")
			label.place(x=100, y=20*n + 50)
		
		#Added label handle
		hLabel.append(label)
	

#
#Dynamically create checkboxes
#
def make(ebent):
	#Get the number of checkboxes to create (Entry value)
	num = Entry1.get()
	
	#Delete existing checkboxes and labels
	for n in range(len(hCheck)):
		hCheck[n].destroy()
		hLabel[n].destroy()
	
	#Empty the array
	del CheckVal[:]
	del hCheck[:]
	del hLabel[:]
	
	#Loop for the value entered in Entry1
	for n in range(int(num)):
		#Creating a BooleanVar
		bl = Tkinter.BooleanVar()
		
		#Determine the value of the checkbox
		bl.set(False)
		
		#Creating a checkbox
		b = Tkinter.Checkbutton(text = "item" + str(n+1), variable = bl)
		b.place(x=5, y=20*n + 50)
		
		#Add the value of the checkbox to the list
		CheckVal.append(bl)
		
		#Add checkbox handle to list
		hCheck.append(b)
	


button1 = Tkinter.Button(root, text=u'Creating a Checkbutton',width=20)
button1.bind("<Button-1>",make)
button1.place(x=90, y=5)

button2 = Tkinter.Button(root, text=u'Get check',width=15)
button2.bind("<Button-1>",check)
button2.place(x=265, y=5)

Entry1 = Tkinter.Entry(root, width=10)
Entry1.place(x=5, y=5)


root.mainloop()

Execution screen of the above code

スクリーンショット 2015-02-23 17.39.52.png

Recommended Posts

Try to dynamically create a Checkbutton with Python's Tkinter
Create a GUI app with Python's Tkinter
Try to create a python environment with Visual Studio Code & WSL
Rails users try to create a simple blog engine with Django
Try to create a Qiita article with REST API [Environmental preparation]
[Cloudian # 3] Try to create a new object storage bucket with Python (boto3)
Create a frame with transparent background with tkinter [Python]
Create a GUI executable file created with tkinter
Try to draw a life curve with python
Try to make a "cryptanalysis" cipher with Python
Create a matrix with PythonGUI (tkinter combo box)
Steps to create a Twitter bot with python
Try to create a new command on linux
Try to make a dihedral group with Python
How to create a multi-platform app with kivy
Try drawing a map with Python's folium package
Try using Python's Tkinter
Try to create a Todo management site using WebSocket with Django (Swamp Dragon)
I tried to create a table only with Django
Try to make a command standby tool with python
Create a native GUI app with Py2app and Tkinter
How to create a submenu with the [Blender] plugin
[GCP] Try a sample to authenticate users with Firebase
A sample to try Factorization Machines quickly with fastFM
I want to manually create a legend with matplotlib
[Python] How to create a 2D histogram with Matplotlib
[Python] Create a Tkinter program distribution file with cx_Freeze
Try to create a waveform (audio spectrum) that moves according to the sound with python
I tried to create a class to search files with Python's Glob method in VBA
Try to factorial with recursion
Create a homepage with django
Create Image Viewer with Tkinter
Create a heatmap with pyqtgraph
Create a directory with python
Try to select a language
[Go language] Try to create a uselessly multi-threaded line counter
How to kill a process instantly with Python's Process Pool Executor
I tried to automatically create a report with Markov chain
Create a Mastodon bot with a function to automatically reply with Python
Probably the easiest way to create a pdf with Python3
Create a life game that is manually updated with tkinter
[Learning record] Create a mysterious dungeon game with Pyhton's Tkinter
How to create a flow mesh around a cylinder with snappyHexMesh
How to print characters as a table with Python's print function
Try to bring up a subwindow with PyQt5 and Python
Let's create a function to hold down Button in Tkinter
Create a message corresponding to localization with python translation string
[C language] [Linux] Try to create a simple Linux command * Just add! !!
Try to draw a Bezier curve
Create a python GUI using tkinter
Steps to create a Django project
Create folders from '01' to '12' with python
Try to operate Facebook with Python
I wanted to create a smart presentation with Jupyter Notebook + nbpresent
Try to create an execution path diff viewer with angr + bingraphvis
How to create a Conda package
Create a virtual environment with Python!
Try to extract a character string from an image with Python3
Try to profile with ONNX Runtime
I tried to create a list of prime numbers with python
How to create a virtual bridge