Python Forum
[Tkinter] How to set new opened form activate and minimize main from after new for opened?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to set new opened form activate and minimize main from after new for opened?
#1
Hi everyone, my new from is in front of my main form but its not activate! I have to click on in to become activate.
How can I handle it ?
Is it possible to minimize the main form when new one opens?

here is my code to open new form from clicking a button in python

def carrepfrom():
    car_report_form = Tk()
    car_report_form.title("فرم گزارش گیری از ماشین آلات")
    car_report_form.resizable(width=False, height=False)
    car_report_form.geometry('500x500')
    btn1 = Button(car_report_form, text='test')
    btn1.place(x=10, y=10)
    car_report_form.mainloop()

car_report_btn = Button(win, text='گزارش گیری از ماشین آلات', command=lambda: carrepfrom())
car_report_btn.place(x=560, y=130)
Reply
#2
jalal0034 likes this post
Reply
#3
I changed my code to tkinter.Toplevel but steel the problem appears!!
the secend form is not activate and messegeboxes shown on my old form (main form)
Reply
#4
What do you mean by this?
Quote:the secend form is not activate
Does the second window not appear? It must be that you are not calling the function that creates the toplevel window.
Quote:messegeboxes shown on my old form (main form)
What are "messegeboxes"? I think of a message box as a popup window that displays a message. It is a dialog style window that disappears when you press the Ok button. Do you mean adding widgets like Label, Text or Entry? These must be added to the new window just like the button. You must pass the window to parent the widget as an argument in the create call (i.e. tk.Label(new_window, text="This is text in the new window"). When you don't specify a window for a widget, the widget is added to the root window.
tk.Label(text="This will appear in the root window").pack()
tk.Label(popup, text="This will appear in the popup window").pack()
This code makes two windows with buttons displaying the other window:
import tkinter as tk

class PopupWindow(tk.Toplevel):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.parent = parent
        button = tk.Button(self, text="This is the popup window", command=self.button_callback)
        button.pack(padx=50, pady=50)

    def button_callback(self):
        self.parent.deiconify()
        self.withdraw()


class MainWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.popup_window = PopupWindow(self)
        self.popup_window.withdraw()
        button = tk.Button(self, text="This is the root window", command=self.button_callback)
        button.pack(padx=50, pady=50)

    def button_callback(self):
        self.popup_window.deiconify()
        self.withdraw()

MainWindow().mainloop()
You may have a good reason for making our interface work this way, but generally this is not a good way to design a user interface. Most user interfaces have only one main window that changes the view to perform different tasks. Any other windows are dialogs that appear for a short time to accomplish a specific task, like a file open dialog.
jalal0034 likes this post
Reply
#5
Thanks deanhystad for your time. I'm amatour in programming. I dont know about changing view , How can I do it?
I simply code this way in command of a button to show a new window and do some work like reporting , add, edit and etc for my database cars
here it is:
#its my main form 
win=Tk()
# in this block the user will add information of his company cars
#
#new form for reporting
def report_form():
     rep_form=tk.Toplevel()
     rep_form.mainloop()

# a button to show a new form for get any report of cars
btn1=Button(win,text='report',command=report_form)
win.mainloop()
after clicking the btn1 , it will show the rep_form . problem is here , the first form (win) steel is active form and the user must click on the second form (rep_form) to add data and use it

How can I handle it?
Reply
#6
https://www.tutorialspoint.com/how-to-se...in-tkinter
jalal0034 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  neeed to activate a custom tk slider janeik 1 832 Oct-09-2023, 09:29 AM
Last Post: deanhystad
  [Tkinter] how to activate Enter key in text box astral_travel 3 2,669 Oct-17-2020, 05:56 PM
Last Post: astral_travel
  “main thread is not in main loop” in Tkinter Long_r 1 24,249 Jun-26-2019, 11:00 PM
Last Post: metulburr
  [Tkinter] Tkinter window minimize,appear frequency 5 14,122 Dec-28-2018, 02:02 PM
Last Post: frequency
  GTK main window calling a main window DennisT 4 6,790 Oct-19-2016, 09:36 PM
Last Post: DennisT

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020