0

I am having data in a text file.

AAA
BBB
CCC

111
222
333

!!!
@@@
###

XXX
YYY
ZZZ

No issue to read the file and able to assigned dict. Dict consist of key and pair list. One of the random keys selected and from the associated list a random line will pick up as a question. Associated question with the label.

The remaining element became the answers. Tried to set up a dynamic checkbox but was unable to pause the execution. For now, the idea is to get value via Checkbuttons and if it's part of the same pair, it should move to the next iteration.

I tried few things but was unable to handle return value. Not my method is correct. Should I use cmd button instead of waiting for Checkbutton clicks?

enter image description here

Here is my revised code:

import tkinter
from tkinter import *
import random

window = Tk()

def quiz():
        chkValue = IntVar() 
        QL = Label(window,text = (Q), bg = "Blue").pack()
        for x in range(len(cpll[key])):
            chkValue[x] =  Variable()
            print(cpll[key][x])
            QA[x] = Checkbutton(window,text = cpll[key][x],var=chkValue[x], command=nextq).pack()[x],var=chkValue, command=nextq).pack()

def nextq():
        pass
# Using readlines()
file1 = open('cpll.txt', 'r')
lines = file1.readlines()
cpll ={}
count = 0
idx = 0

for line in lines:
    if line == "\n":
        idx +=1
        count += 1
    else:
        cpll.setdefault(idx,[]).append(line.strip())
        count += 1

for key, values in cpll.items():
    print(key)
    
    if(isinstance(values, list)):
        for value in values:
            print(value)
            
    else:
        print(value)

for value in cpll.values():
    print(value)
    
    
key_list = list(cpll)

random.shuffle(key_list)

d2 = {} 


for key in key_list:

        d2[key] = cpll[key]
        Q = random.choice(cpll[key])
        cpll[key].remove(Q)        

quiz()
window.mainloop()
user1582596
  • 503
  • 2
  • 5
  • 16
  • 1
    Tkinter GUI programs don't execute [imperatively](https://en.wikipedia.org/wiki/Imperative_programming) the way you're probably used to programming. They are [event-driven](https://en.wikipedia.org/wiki/Event-driven_programming) — a completely different programming paradigm. See the answer to [Tkinter — executing functions over time](https://stackoverflow.com/questions/9342757/tkinter-executing-functions-over-time) for more information. – martineau Oct 15 '21 at 16:19
  • 1
    Also note that `Checkbutton`s don't pause or `return` values. – martineau Oct 15 '21 at 16:21

1 Answers1

1

Checkbuttons and other GUI widgets are not synchronous. You can't really wait for something to happen, but you can always check a value. GUIs are event-driven. So you can always use a change of widget state to trigger an action.

The Checkbutton widget has a variable option, which allows you to link that Checkbutton widget to a tkinter IntVar() integer variable. Then THAT IntVar can be traced to trigger a callback action whenever the IntVar is read from or written to. Once the Checkbutton widget is linked to the IntVar, changing the value of the IntVar will update the visual appearance of the Checkbutton, and clicking on the Checkbutton will update the value of the IntVar, triggering any callback function.

Here's an excerpt on the Checkbutton variable option from https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/checkbutton.html:

The control variable that tracks the current state of the checkbutton; see Section 52, “Control variables: the values behind the widgets”. Normally this variable is an IntVar, and 0 means cleared and 1 means set, but see the offvalue and onvalue options above.

For more on IntVar(), check out https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/control-variables.html

GaryMBloom
  • 5,350
  • 1
  • 24
  • 32