Home>


1. After selecting the band with the radio button, select the check box,
I want to create a window that always appears in front when the [Start Measurement] button is pressed.

2, I want to create a "stop" button in the subwindow (button creation code is created but not reflected)

■ Current status
When the [Measurement Start] button is pressed, the subwindow that is to be displayed first is displayed last.
I want to create a button in a subwindow, but the code doesn't reflect it.

from tkinter import *
from tkinter import ttk
from time import sleep
root = Tk ()
root.title ('Measurement CH setting')
root.geometry ("500x860 + 710 + 40")
Lframe1 = ttk.Labelframe (root, text = 'measurement band selection', padding = 5)
Lframe1.grid (row = 0, column = 0, columnspan = 1, padx = 15, pady = 10, sticky = 'w')
Lframe2 = ttk.Labelframe (root, text = '2.4GHz', padding = 5)
Lframe2.grid (row = 1, column = 0, columnspan = 1, padx = 15, pady = 10, sticky = 'nw')
Lframe3 = ttk.Labelframe (root, text = '5GHz', padding = 5)
Lframe3.grid (row = 1, column = 1, columnspan = 1, padx = 10, pady = 10, sticky = 'w')
selected_frame = IntVar ()
def change_selected_frame ():
    if selected_frame.get () == 0:
        for child in Lframe2.winfo_children ():
            child.configure (state = 'enable')
        for child in Lframe3.winfo_children ():
            child.configure (state = 'disable')
    elif selected_frame.get () == 1:
        for child in Lframe2.winfo_children ():
            child.configure (state = 'disable')
        for child in Lframe3.winfo_children ():
            child.configure (state = 'enable')
    else:
        for child in Lframe2.winfo_children ():
            child.configure (state = 'disable')
        for child in Lframe3.winfo_children ():
            child.configure (state = 'disable')
Rbutton1 = ttk.Radiobutton (Lframe1, text = '2.4GHz', variable = selected_frame, value = 0, command = change_selected_frame)
Rbutton1.grid (row = 1, column = 0, pady = 5)
Rbutton2 = ttk.Radiobutton (Lframe1, text = '5GHz', variable = selected_frame, value = 1, command = change_selected_frame)
Rbutton2.grid (row = 1, column = 1, pady = 5)
class val_2G:
    def __init __ (self, name, set_frame, row_no):
        self.bool = BooleanVar ()
        self.bool.set (False)
        self.name = name
        self.Cbtn = ttk.Checkbutton (set_frame, text = self.name, variable = self.bool)
        self.Cbtn.grid (row = row_no, column = 0, columnspan = 1, padx = 15, pady = 5, sticky = 'w')
class val_5G:
    def __init __ (self, name, set_frame, row_no):
        self.bool = BooleanVar ()
        self.bool.set (False)
        self.name = nameself.Cbtn = ttk.Checkbutton (set_frame, text = self.name, variable = self.bool)
        self.Cbtn.grid (row = row_no, column = 0, columnspan = 1, padx = 15, pady = 5, sticky = 'w')
val_2G1 = val_2G ('CH1', Lframe2,6)
val_2G2 = val_2G ('CH2', Lframe2,7)
val_2G3 = val_2G ('CH3', Lframe2,8)
val_2G4 = val_2G ('CH4', Lframe2,9)
val_2G5 = val_2G ('CH5', Lframe2,10)
val_2G6 = val_2G ('CH6', Lframe2,11)
val_2G7 = val_2G ('CH7', Lframe2,12)
val_2G8 = val_2G ('CH8', Lframe2,13)
val_2G9 = val_2G ('CH9', Lframe2,14)
val_2G10 = val_2G ('CH10', Lframe2,15)
val_2G11 = val_2G ('CH11', Lframe2,16)
val_2G12 = val_2G ('CH12', Lframe2,17)
val_2G13 = val_2G ('CH13', Lframe2,18)
val_2G_list = [val_2G1, val_2G2, val_2G3, val_2G4, val_2G5, val_2G6, val_2G7, val_2G8, val_2G9, val_2G10, val_2G11, val_2G12, val_2G13]
val_5G36 = val_5G ('CH36', Lframe3,6)
val_5G40 = val_5G ('CH40', Lframe3,7)
val_5G44 = val_5G ('CH44', Lframe3,8)
val_5G48 = val_5G ('CH48', Lframe3,9)
val_5G52 = val_5G ('CH52', Lframe3,10)
val_5G56 = val_5G ('CH56', Lframe3,11)
val_5G60 = val_5G ('CH60', Lframe3,12)
val_5G64 = val_5G ('CH64', Lframe3,13)
val_5G100 = val_5G ('CH100', Lframe3,14)
val_5G104 = val_5G ('CH104', Lframe3,15)
val_5G108 = val_5G ('CH108', Lframe3,16)
val_5G112 = val_5G ('CH112', Lframe3,17)
val_5G116 = val_5G ('CH116', Lframe3,18)
val_5G120 = val_5G ('CH120', Lframe3,19)
val_5G124 = val_5G ('CH124', Lframe3,20)
val_5G128 = val_5G ('CH128', Lframe3,21)
val_5G132 = val_5G ('CH132', Lframe3,22)
val_5G136 = val_5G ('CH136', Lframe3,23)
val_5G140 = val_5G ('CH140', Lframe3,24)
val_5G_list = [val_5G36, val_5G40, val_5G44, val_5G48, val_5G52, val_5G56, val_5G60, val_5G64, val_5G100, val_5G104, val_5G108, val_5G112, val_5G112, val_5G112, val_5G

def check_all_2G_checkboxes ():
    for val in val_2G_list:
        val.bool.set (True)
def clear_all_2G_checkboxes ():
    for val in val_2G_list:
        val.bool.set (False)
def check_all_5G_checkboxes ():
    for val in val_5G_list:
        val.bool.set (True)
def clear_all_5G_checkboxes ():
    for val in val_5G_list:
        val.bool.set (False)
def sub_window ():
    sub_window = Toplevel ()
    sub_window.title ('Emergency stop')sub_window.attributes ("-topmost", True)
    sub_window.geometry ("600x300 + 660 + 400")
    btn = ttk.Button (text = 'stop', padding = 5) ## Button is not created
def check_box ():
    global checkbox
    sub_window () ## I want to display it at the very beginning and move it like an emergency stop button, but it will be displayed at the very end
    selected_channels2 = []
    selected_channels5 = []
    if selected_frame.get () == 0:
        for val in val_2G_list:
            if val.bool.get ():
                selected_channels2.append (val.name)
    elif selected_frame.get () == 1:
        for val in val_5G_list:
            if val.bool.get ():
                selected_channels5.append (val.name)
    if len (selected_channels2)>0:
        measure_channels2 (selected_channels2)
    elif len (selected_channels5)>0:
        measure_channels5 (selected_channels5)
def measure_channels2 (channels2):
    for channel2 in channels2:
        ch2 = channel2 [2:]
        print (f'measuring channel {ch2} ')
        sleep (5)
        print ("Measurement end")
def measure_channels5 (channels5):
    for channel5 in channels5:
        ch5 = channel5 [2:]
        print (f'measuring channel {ch5} ')
        sleep (5)
        print ("Measurement end")
def quit ():
    global checkbox
    checkbox.destroy ()
btn1 = ttk.Button (Lframe2, text = 'Select all', padding = 5, command = check_all_2G_checkboxes)
btn1.grid (row = 5, column = 0, pady = 5)
btn2 = ttk.Button (Lframe2, text = 'Clear all', padding = 5, command = clear_all_2G_checkboxes)
btn2.grid (row = 5, column = 1, pady = 5)
btn3 = ttk.Button (Lframe3, text = 'Select all', padding = 5, command = check_all_5G_checkboxes)
btn3.grid (row = 5, column = 0, pady = 5)
btn4 = ttk.Button (Lframe3, text = 'Clear all', padding = 5, command = clear_all_5G_checkboxes)
btn4.grid (row = 5, column = 1, pady = 5)
btn5 = ttk.Button (text = 'Start measurement', padding = 5, command = check_box)
btn5.grid (row = 5, column = 0, pady = 5)
btn6 = ttk.Button (text = 'Cancel', padding = 5, command = quit)
btn6.grid (row = 5, column = 1, pady = 5)
selected_frame.set (-1)
change_selected_frame ()
root.mainloop ()

If i know how to write, I would appreciate it if you could teach me.

  • Answer # 1

    25 days have passed since you submitted your question.

Related articles