0

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.

Application Behavior

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()
martineau
  • 119,623
  • 25
  • 170
  • 301
pcummings
  • 11
  • 3
  • FWIW, I get a `_tkinter.TclError: wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??" from the `app.attributes('-zoomed', True)` line of your code. If I change it to `app.wm_state('zoomed')` the error goes away and the `top.attributes('-topmost', 'true')` later on seems to work — i.e. the `Toplevel` window is visible, it's on top, and it stays that way. – martineau Dec 11 '21 at 22:04
  • @martineau Thank you for you insight. I had the same error now and was bale to the code to behave by making these changes: `# Create a GUI app app = Tk() # Give a title to your app app.title("Auger Fill 1.0") app.overrideredirect(False) app.wm_state('normal') app.attributes('-zoomed', True)` – pcummings Dec 19 '21 at 04:09
  • @martineau Do you have any thoughts about why `hmiBttn = Button(settingsFrame, text="HMI", command = open_HMIConfig("Anything"))` causes the function HMIConfig() to be executed immediately? I'm trying to pass a value into the function but it launches the function on the first parsing of the code and causes the HMIconfig window to launch at the same time as the main window. – pcummings Dec 19 '21 at 04:19
  • Yes, I do. See [Why is the command bound to a Button or event executed when declared?](https://stackoverflow.com/questions/5767228/why-is-the-command-bound-to-a-button-or-event-executed-when-declared) To fix it change the last argument to `command = lambda: open_HMIConfig("Anything")`. – martineau Dec 19 '21 at 08:11
  • 1
    Thank you for the link. I had missed that. Cleared up a tremendous amount. Everything is working well now. Very valuable insights! – pcummings Dec 26 '21 at 22:57
  • That's good to hear. Happy coding! – martineau Dec 26 '21 at 23:35

0 Answers0