ayush bajpai
ayush bajpai

Reputation: 11

How do I disable menu button when a child window opens when it is clicked, until the child window is closed in tkinter

I have created a menu bar on the parent window. And I have made a child window as userguide.py which is assigned to one of the button on the menu that is "USER GUIDE". When I click on the button the child window opens, but when again clicked it opens one more window and so continues if clicked on the menu button. How do I disable the menu button until the child window is closed. I am sharing the code below.

Below this is code of the menu in parent window

from tkinter import *
from tkinter import ttk
from userguide import userGuide 

root = Tk()
root.title("GhostTube - by RuSher")
root.iconbitmap(r"C:\Users\ayush\Desktop\BOTRAW\main\ghost.ico")
root.maxsize(400, 685)
root.minsize(400, 685)
root.geometry("400x685")

style = ttk.Style()
style.theme_use("clam")
menubar = Menu(root) 
menubar.add_command(label="USER GUIDE    I", command=userGuide)
menubar.add_command(label="ABOUT    I")
menubar.add_command(label="CONTACT", command=contactInfo)

root.config(menu=menubar)
root.mainloop()`

Menu bar image

Below given code is the child window code i.e. userguide.py

from tkinter import *
from tkinter import ttk

def userGuide():

    master = Tk()
    master.title("GhostTube - by RuSher")
    master.iconbitmap(r"C:\Users\ayush\Desktop\BOTRAW\main\ghost.ico")
    master.maxsize(400, 685)
    master.minsize(400, 685)
    master.geometry("400x685")

    style = ttk.Style()
    style.theme_use("clam")


    userLabel = Label(master, text="                  HOW TO USE                  ", bg="black", fg="white", font=("Elephant", 18))
    userLabel.grid(pady=10)

    guide1Frame = LabelFrame(master, text="STEP 1 :", padx=5, pady=10)
    guide1Frame.grid(row=1, column=0, padx=5)

    step1label = Label(guide1Frame, text="First step is to add accounts to the bot, for that click on \n“Load Accounts” then locate the “YT accounts.txt” file and select it.\nIt will load all the accounts in the file.")
    step1label.grid()

    guide2Frame = LabelFrame(master, text="STEP 2 :", padx=5, pady=10)
    guide2Frame.grid(row=2, column=0, padx=5)

    step2label = Label(guide2Frame, text="Add the video URL of which you want to boost. Type or copy paste \nyour video URL on (Type Here) space and click on “Add”. \nOr you can make a text file of your video URLs and load it \nby clicking on “Load Video URLs” and selecting the text file.")
    step2label.grid()

    guide3Frame = LabelFrame(master, text="STEP 3 :", padx=5, pady=10)
    guide3Frame.grid(row=3, column=0, padx=5)

    step3label = Label(guide3Frame, text="Now you need to select the action you want to have \non the video to be performed, that are: LIKE, DISLIKE, \nSUBSCRIBE, UNSUBSCRIBE or COMMENT. You can select multiple \nactions at a time. After completing this Click on “START” button.")
    step3label.grid()

    guide4Frame = LabelFrame(master, text="STEP 4 :", padx=5, pady=10)
    guide4Frame.grid(row=4, column=0, padx=5)

    step4label = Label(guide4Frame, text="For Comments, You can Type or copy paste comments on\n(Type Here) of your choice, one comment at a time then click\non “Add” button and then type next comment of your choice then\nclick on “Add” and repeat if you want more. Or you can make\na text file of comments of your choice (one comment per line)\nand load it by clicking on “Load Comments”and locate your\ncomments text file and select it. Then click on “START”.")
    step4label.grid()

    guide5Frame = LabelFrame(master, text="STEP 5 :", padx=5, pady=10)
    guide5Frame.grid(row=5, column=0, padx=5)

    step5label = Label(guide5Frame, text="When the bot runs first time it can take time because of it prepares\nthe system, after the process starts please do not intercept it.\nLet it complete all the actions. The process goes in steps; \nfirst, a CMD interface will open then a Firefox window will start and\nit will complete one action then the Firefox window will close and\nthe same steps will repeat according to the actions to be taken.")
    step5label.grid()

    quote = Frame(master, padx=5, pady=10)
    quote.grid(row=6, columnspan=2)

    enjoy = Label(quote, text="Experience The Best", font=("Elephant",16), bg="black", fg="white")
    enjoy.grid(padx=5)

    master.mainloop()

Please help me out, because I am a fresh starter in this topic.

Upvotes: 0

Views: 394

Answers (1)

TheLizzard
TheLizzard

Reputation: 7680

Try something like this:

import tkinter as tk

def create_window(root):
    # Create the new window
    top = tk.Toplevel(root)
    # Stop the user from interacting with the main window
    top.grab_set()

root = tk.Tk()

menubar = tk.Menu(root) 
menubar.add_command(label="Create new window", command=lambda: create_window(root))
root.config(menu=menubar)

root.mainloop()

It uses top.grab_set() to stop the user from interacting with the main window. It blocks all events until the user closes the pop up.

Upvotes: 1

Related Questions