0

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.

  • Instead of `Tk()` use `Toplevel()` in your `userGuide` function. If you do that you can use `.grab_set()` to stop the user from interacting with the main window. – TheLizzard Jun 12 '21 at 13:11

1 Answers1

1

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.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • I got this, but if you notice, the menubar code is in my main.py file, and i have imported userguide.py in main.py , so how do i manage that. I have made my new window that is userguide as a function. – ayush bajpai Jun 12 '21 at 14:05
  • @ayushbajpai You can pass it into the function using: `.add_command(..., command=lambda: userGuide(root))`. I edited my answer – TheLizzard Jun 12 '21 at 14:06
  • ok so you mean that instead of making the menu bar in the main.py file, i should do it in my userguide.py and import it in the main.py – ayush bajpai Jun 12 '21 at 14:09
  • No I mean when you call the function in your `userguide.py` file, just pass in the `root` as a parameter. – TheLizzard Jun 12 '21 at 14:10
  • ohh ok ok thank you, i got that now. thank you for your help @TheLizzard – ayush bajpai Jun 12 '21 at 14:12
  • Brother , i did what you said. but that is making my parent window disabled when the child window opens. What i want to do is, disable my menu bar or the particular button which pressed and the child window opens, until the child window is open, the menubar or the menu button should be disabled . so that no new window opens when its clicked while already a child window is open. i have edited the code, added a complete parent window code and the child window code. please look into it. Thank you – ayush bajpai Jun 12 '21 at 14:30
  • My code disables all user input from reaching the main window (which is what you will need most of the time). If you want to only disable the menu, look at [this](https://stackoverflow.com/a/54798728/11106801). It suggests that you use `menu.entryconfigure("USER GUIDE I", state="disabled")` to disable the menu and `menu.entryconfigure("USER GUIDE I", state="normal")` to re-enable it – TheLizzard Jun 12 '21 at 14:34