I'm new to tkinter/python and am coding a simple interface to a bagger unit.
The main app interface has a timer window that takes a value and a button that brings up a toplevel window that allows the operator to store some parameters.
So far, I have been unable to get this window to display in front of the main window.
I am looking for a way to make the top level window open_HMIConfig
display on top and only when called by the button hmiBttn
.
I have added a stripped down version of the code below.
Note the use of the top.attributes('-topmost', 'true')
statement in the def for the top level window.
# Import the library tkinter
import tkinter as tk
from tkinter import *
# Create a GUI app
app = Tk()
app.title("My Simple GUI 1.0")
app.attributes('-zoomed', True)
# Declare global variables
jobtime_ms = None
SettingHeightWidth = "350x200"
BttnColor = "grey"
title = None
# HMI CONFIG WINDOW
def open_HMIConfig(title):
print ('open_HMIConfig called')
top=Toplevel(app)
top.attributes('-topmost', 'true')
top.grab_set()
# get main window position
root_x = app.winfo_rootx()
root_y = app.winfo_rooty()
# add offset
win_x = root_x + 75
win_y = root_y + 75
# set toplevel in new position
top.geometry(SettingHeightWidth)
top.geometry(f'+{win_x}+{win_y}')
top.title(title)
top.configure(bg="light grey")
Button(top, text="Quit", command=top.destroy).pack(side = BOTTOM, expand = 1, fill = BOTH, padx=10, pady=10)
AUGERSettingsFrame = LabelFrame(top, text="SETTINGS", bg="light grey", fg="white")
AUGERSettingsFrame.pack(side = BOTTOM, fill = BOTH, expand = 1, padx=10, pady=10)
#****PAGE FRAMES****
# TOP FRAME
# Constructing the topFrame
topFrame = Frame(app, bg="grey", padx=15, pady=15)
# Displaying the topFrame
topFrame.pack(side = TOP, fill = BOTH, expand = 1)
#****WIDGET FRAMES****
# Construct jobFrame - HOLDS JOB WIDGETS
jobFrame = LabelFrame(topFrame, text="JOB TIME (ms)", bg="grey",
fg="white", padx=15, pady=5 )
# Display jobFrame
jobFrame.pack(side = LEFT, fill = BOTH, expand = 1)
# Construct JOB LEFT FRAME
jobleftFrame = Frame(jobFrame, bg="grey", padx=5, pady=5)
# Display JOB LEFT FRAME
jobleftFrame.pack(side = LEFT, fill = BOTH, expand = 1)
# Construct JOB RIGHT FRAME
jobrightFrame = Frame(jobFrame, bg="grey", padx=5, pady=5)
# Display JOB RIGHT FRAME
jobrightFrame.pack(side = RIGHT, fill = BOTH, expand = 1)
# Constructing the SETTINGS FRAME - HOLDS SETTINGS WIDGETS
settingsFrame = LabelFrame(topFrame, text="SETTINGS", bg="grey",
fg="white", padx=15, pady=15)
# Displaying the settingsFrame
settingsFrame.pack(side = RIGHT, fill = BOTH, expand = 1)
# Construct the buttons in settingsFrame
hmiBttn = Button(settingsFrame, text="HMI", command = open_HMIConfig("Anything"))
# Display the buttons in settingsFrame
hmiBttn.pack(side = BOTTOM, fill = BOTH, expand = 1)
# Constructing the widgets in jobFrame
AugerRunTime = Entry(jobleftFrame, text="AUGER", textvariable = jobtime_ms, font=('Calibri', 24, 'bold'), justify='center')
minuBttn = Button(jobrightFrame, text="-")
plusBttn = Button(jobrightFrame, text="+")
# Displaying the buttons in jobFrame
AugerRunTime.pack(side = LEFT, fill = BOTH, expand = 1)
minuBttn.pack(side = BOTTOM, fill = BOTH, expand = 1)
plusBttn.pack(side = BOTTOM, fill = BOTH, expand = 1)
#Make the loop for displaying app
app.mainloop()