from tkinter import * from tkinter import ttk import settings as g import cage_func as func import random as rand NORM_FONT = () SUB_HEADER_FONT = ("Arial", 9, "bold") class HelmholtzGUI(Tk): def __init__(self): Tk.__init__(self) Tk.wm_title(self, "Helmholtz Cage Control") Tk.wm_iconbitmap(self, "Helmholtz.ico") self.Menu = TopMenu(self) # displays menu bar at the top mainArea = Frame(self) mainArea.pack(side="top", fill="both", expand=False) mainArea.grid_rowconfigure(0, weight=1) mainArea.grid_columnconfigure(0, weight=1) self.frames = {} # dictionary for storing all pages for F in (TestFrame, StatusDisplay): frame = F(mainArea, self) self.frames[F] = frame frame.grid(row=0, column=0, sticky="nsew") self.show_frame(StatusDisplay) def show_frame(self, key): frame = self.frames[key] # gets correct page from the dictionary frame.tkraise() # brings this frame to the front class TopMenu: def __init__(self, window): print("menu called") menu = Menu(window) window.config(menu=menu) ModeSelector = Menu(menu) menu.add_cascade(label="Mode", menu=ModeSelector) ModeSelector.add_command(label="Full Manual", command=self.manual_mode) def manual_mode(self): print("Switching to manual mode") class TestFrame(Frame): def __init__(self, parent, controller): Frame.__init__(self, parent) print("TestFrame called") one = Label(self, text="One", bg="red") one.pack(fill=X) two = Label(self, text="Two", bg="blue") two.pack() button = ttk.Button(self, text="Print stuff", command=lambda: print_stuff("Hello")) button.pack() class StatusDisplay(Frame): def __init__(self, parent, controller): Frame.__init__(self, parent) print("StatusDisplay called") # ToDo: remove rowCounter = 0 # keep track of which row we are at in the grid layout col = 0 for header in ["X-Axis", "Y-Axis", "Z-Axis"]: # create Column headers headLabel = Label(self, text=header, font=SUB_HEADER_FONT) headLabel.grid(columnspan=2, row=rowCounter, column=col*2, sticky="ew") col = col + 1 # move to next column rowCounter = rowCounter + 1 # increase row counter to place future stuff below header # define content of row entries TextLabels = ["Serial Port:", "PSU Channel:", "Connection Status:", "Output:", "Remote Control:", "Voltage Setpoint:", "Actual Voltage:", "Current Setpoint:", "Actual Current:", " ", "Target Field:", "Trgt. Field Raw:", "Target Current:", "Inverted:"] self.rowNo = len(TextLabels) # get number of label rows self.columnNo = 6 # number of label columns # prepare list of lists to contain all labels for row entries in all columns: self.Labels = [[] for _ in range(self.columnNo)] self.label_dict = {} for name in TextLabels: self.label_dict[name] = [StringVar() for _ in range(int(self.columnNo/2))] for col in range(int(self.columnNo/2)): self.Labels[col*2].append(Label(self, text=name)) self.Labels[col*2+1].append(Label(self, textvariable=self.label_dict[name][col])) col = 0 for LabelCol in self.Labels: # place row entries in grid layout for all columns for row in range(self.rowNo): # place row entries LabelCol[row].grid(row=row+rowCounter, column=col, sticky="w", padx=10) col = col + 1 rowCounter = rowCounter + self.rowNo # increase row counter to place future stuff below this self.update_labels(controller) def update_labels(self, controller): i = 0 for axis in g.AXES: if axis.device is not None: axis.update_values() self.label_dict["Serial Port:"][i].set(g.ports[i]) self.label_dict["PSU Channel:"][i].set(axis.channel) self.label_dict["Connection Status:"][i].set(axis.connected) self.label_dict["Output:"][i].set(axis.output_active) self.label_dict["Remote Control:"][i].set(axis.remote_ctrl_active) self.label_dict["Voltage Setpoint:"][i].set("%0.3f V" % axis.voltage_setpoint) self.label_dict["Actual Voltage:"][i].set("%0.3f V" % axis.voltage) self.label_dict["Current Setpoint:"][i].set("%0.3f A" % axis.current_setpoint) self.label_dict["Actual Current:"][i].set("%0.3f A" % axis.current) self.label_dict["Target Field:"][i].set("%0.3f \u03BCT" % (axis.target_field*1e6)) self.label_dict["Trgt. Field Raw:"][i].set("%0.3f \u03BCT" % (axis.target_field_comp*1e6)) self.label_dict["Target Current:"][i].set("%0.3f A" % axis.target_current) self.label_dict["Inverted:"][i].set(axis.polarity_switched) i = i+1 controller.after(2000, lambda: self.update_labels(controller)) def print_stuff(stuff): print(stuff) def random_no(): return rand.uniform(0, 20) class TestValues: def __init__(self): self.val1 = 0 self.val2 = 0 self.val3 = 0 def update_values(self): self.val1 = rand.uniform(0, 20) self.val2 = rand.uniform(0, 20) self.val3 = rand.uniform(0, 20)