The attached customtkinter program jumps to the end print statements before any selection from the comboboxes. How can I control the program execution so the user selects the baudrate and port from the comboboxes and then the tk.IntVar and tk.String.Var are updated to be used in another section of the program that will connect to a serial port with the baudrate and port tk.variables
also how do I get the 4 space indent on the code below so others won’t need to correct it thanks
type here#
#
import os
import os.path
import sys
import time
import tkinter as tk
import tkinter.ttk as ttk
from ast import literal_eval
from tkinter import *
# from tkinter import CENTER, constants
from tkinter import messagebox, ttk
import threading
import customtkinter
import serial
import serial.tools.list_ports
import tkinter
import glob
customtkinter.set_appearance_mode('dark')
customtkinter.set_default_color_theme("blue")
#lock = threading.Lock()
def baud_rate():
#global b_rates
b_rates = ("9600", "19200", "38400", "57600", "115200")
#print('baud rate list is ',b_rates)
return b_rates
def serial_ports():
""" thanks to the original function writer don't remember
where it's from many months ago
"""
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
def update_baud():
while baud_set == None:
baud_val_set = baud_combobox.get()
baud_set.set(baud_val_set)
print('baud_set = ',baud_set)
def update_port():
while port_set == None:
port_val_set = port_combobox.get()
port_set.set(port_val_set)
print('port_set = ',port_set)
app = customtkinter.CTk()
port_str_in = tk.StringVar()
port_str_in = serial_ports()
baud_str_in = tk.StringVar()
baud_str_in = baud_rate()
baud_set = tk.IntVar(app)
port_set = tk.StringVar(app)
def port_sel(*args):
global port_set
port_sel_val = port_combobox.get()
port_set = port_sel_val
print('Port value is ',port_combobox.get())
#print(port_set)
#return port_sel_val
return port_set
def baud_sel(*args):
global baud_set
baud_set = 0
baud_sel_val = baud_combobox.get()
s_sel_baud = int(baud_sel_val)
baud_set = s_sel_baud
print('Int value of baudrate is ',baud_combobox.get())
#print(baud_set)
#return s_sel_baud
return baud_set
update_baud()
update_port()
frame = customtkinter.CTkFrame(master=app,
width=350,
height=350,
corner_radius=10)
frame.pack(padx=20, pady=10)
baud_combobox = customtkinter.CTkComboBox(master=frame,
values=(baud_str_in),
command=baud_sel)
baud_combobox.place(relx=0.25, rely=0.2, anchor=CENTER)
port_combobox = customtkinter.CTkComboBox(master=frame,
values=(port_str_in),
command=port_sel)
port_combobox.place(relx=0.7, rely=0.4, anchor=CENTER)
baud = update_baud()
com = update_port()
print('baud value ',baud)
print('com value ',com)
app.mainloop()
Tried threads, threads with locking, multi-threading. I could have done those wrong. I was expecting a String variable for the port and a int variable for the baudrate to be passed to a serial port connect function.