Python Forum
[Tkinter] Manipulating Checkboxes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Manipulating Checkboxes
#1
Hi, I have this Checkboxes, İmage

any combination can be selected, and each checkbox selected results in a different query. Instead of using if else, can someone suggest an alternative method, because the combination is gonna be endless
[python]
from tkinter import *
import arcpy
from tkinter import ttk

def select_landuse():
    arcpy.env.workspace = C:/Users/umroot/Documents/ArcGIS/Projects/RecentINSE6421project/affectationpu"
    in_features = "AffectationPU.shp"
    out_feature_class = "C:/Users/umroot/Documents/ArcGIS/Projects/RecentINSE6421project/pythonOutput/affectationpu"
    if (landuse1.get() and landuse3.get() and landuse4.get() and landuse5.get()and landuse6.get()and landuse7.get() and landuse10.get())==TRUE :
        where_clause= "\"categorie\" IN (\'activites diversifiees\',\'conservation\', \'emplois\', \'infrastructure\', \'institution\', \'mixte\', \'residentiel\')"

    elif(landuse1.get() and landuse3.get()and landuse4.get() and landuse5.get() and landuse6.get() and landuse7.get() and landuse8.get() and landuse9.get() and landuse10.get())==TRUE:
     where_clause="\"categorie\" IN (\'activites diversifiees\',\'conservation\', \'emplois\', \'infrastructure\', \'institution\', \'mixte\', \'residentiel\')"

    elif (landuse1.get()and landuse2.get() and landuse4.get()and landuse4.get()and landuse6.get()and landuse7.get()and landuse9.get() and landuse10.get())==TRUE:
        where_clause= "\"categorie\" IN (\'activites diversifiees\',\'conservation\', \'emplois\', \'infrastructure\', \'institution\', \'mixte\',\'religieux\',\'residentiel\')"

    elif (landuse1.get()and landuse2.get() and landuse3.get()and landuse4.get()and landuse4.get()and landuse6.get()and landuse7.get()and landuse9.get() and landuse10.get())==TRUE:
        where_clause= "\"categorie\" IN (\'activites diversifiees\',\'conservation\',\'agricole\', \'emplois\', \'infrastructure\', \'institution\', \'mixte\', \'religieux\',\'residentiel\')"


    elif (landuse1.get()and landuse2.get() and landuse3.get()and landuse4.get()and landuse4.get()and landuse6.get()and landuse7.get()and landuse8.get() and landuse10.get())==TRUE:
        where_clause= "\"categorie\" IN (\'activites diversifiees\',\'conservation\',\'agricole\', \'emplois\', \'infrastructure\', \'institution\', \'mixte\', \'parc\',\'residentiel\')"
[/python]
Reply
#2
You could do something like this where controls is all the various checkbox controls ie landuse1.get()
controls = (True, True, False, False, True, True, False, True, False)
clauses = (r"\'activites diversifiees\'", r"\'conservation\'", r"\'agricole\'", r"\'emplois\'",
           r"\'infrastructure\'", r"\'institution\'", r"\'mixte\'", r"\'parc\'", r"\'residentiel\'")
clauses = ', '.join(clause for clause, control in zip(clauses, controls) if control)
where_clause = f"\"categorie\" IN ({clauses})"
print(where_clause)
Output:
"categorie" IN (\'activites diversifiees\', \'conservation\', \'infrastructure\', \'institution\', \'parc\')
Reply
#3
thank Yoriz

but I don't understand how I could use the status (checked or unchecked ie landuse1.get())of the checkboxes in controls without using if /else statements
Reply
#4
Use the above instead of using if /else statements
Reply
#5
Thanks, Yoriz

your the best it worked
Reply
#6
Hi Yoriz
could you make the output like

Output:
\"categorie\" IN (\'activites diversifiees\', \'conservation\', \'infrastructure\', \'institution\', \'parc\')
Reply
#7
Sure
controls = (True, True, False, False, True, True, False, True, False)
clauses = (r"\'activites diversifiees\'", r"\'conservation\'", r"\'agricole\'", r"\'emplois\'",
           r"\'infrastructure\'", r"\'institution\'", r"\'mixte\'", r"\'parc\'", r"\'residentiel\'")
clauses = ', '.join(clause for clause, control in zip(clauses, controls) if control)
where_clause = f'\\"categorie\\" IN ({clauses})'
print(where_clause)
Output:
\"categorie\" IN (\'activites diversifiees\', \'conservation\', \'infrastructure\', \'institution\', \'parc\')
Reply
#8
Thanks Yoriz....
Reply
#9
Hello All,

I need the python code for calculating the value of New_score for a column called ID_TRC using the values of 3 other columns called LENGTH, Category, and Score. The result is to be stored in a newly created new excel file for each unique ID_TRC and its corresponding New_Score. The image is a sample of the data
The rules for calculating the new scores are:

if ID_TRC exists elsewhere in the table, with a different LENGTH and different Category then the

new_score = {SUM((Score X LENGTH))[sum for each rows with the same ID_TRC]}divided by (sum of LENGTHS of all with the same ID_TRC)


if ID_TRC exists elsewhere in the table, with the same LENGTH and different Category then the

new_score = {[u]SUM((Score X LENGTH))[sum for each rows with the same ID_TRC]} divided by LENGTH
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Manipulating an image in python z4rn4v 2 2,112 Jan-02-2019, 12:30 PM
Last Post: z4rn4v
  [Tkinter] Checkboxes in different frames misbehave octoeder 3 4,453 Oct-03-2018, 09:12 AM
Last Post: jfong
  [Tkinter] Print if all checkboxes marked Kaelmi 10 18,492 May-24-2017, 02:17 PM
Last Post: buran
  [WxPython] Setting interdependencies between checkboxes merlem 2 3,619 Feb-12-2017, 07:51 PM
Last Post: merlem

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020