How to code a menu bar across your Pygame project - python

I have been struggling with merging my code.
I am creating an arcade game on Python and have a main file where I have an image and clickable assets which link to a game I have imported.
Now I am working on creating constant features in the game, including a menu bar which displays reminders, can change the volume and brightness setting etcetura.
However, I don't know how to make this on my main project file.
Could you help me out?
I am using Python Pygame, Tkinter and Turtle.

It sounds like you need to think some more about what exactly you want. Your question is rather vague and tkinter has several options that can be presented in various ways to a user for making selections. Below is a quick example of some ideas you may want to explore.tkinter widgets
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("400x400")
root.resizable(False, False)
root.title("Sample")
menu = tk.Menu(root)
root.config(menu=menu)
file_menu = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Quit", command=quit)
file_menu.add_command(label="Something", command=quit)
file_menu.add_command(label="New Option", command=quit)
label = tk.Label(root, text="Choose an option below", font="times 14")#label
label.pack()
items = ["Hi", "Something", "Bye"]
combo_box = ttk.Combobox(root, font=20, state="normal")#combo box
combo_box['values'] = items
combo_box.pack()
lblabel = tk.Label(root, text="Choose an item from the list", font="times 14")#label
lblabel.pack()
items_var = tk.StringVar(value=items)
list_box = tk.Listbox(root, listvariable=items_var, selectmode=tk.BROWSE)#list box
list_box.pack()
rblabel = tk.Label(root, text="Choose an option below", font="times 14")#label
rblabel.pack()
choices = tk.StringVar(value=items)
radio_button = ttk.Radiobutton(root, text="some_option")#radio button
radio_button.pack()
root.mainloop()

Related

tkinter toolbar menu/ button open drop-down menu/gif

In the toolbar I press a button and a drop-down menu with buttons opens. The images are. gif for buttons. I want the button in the toolbar to open a drop-down menu with buttons (image - gif). How can I do this?
# Image Toolbar
img1 = PhotoImage(file = r\path\"img.gif")`
# Toolbar
toolbar = Frame(root, bd=1, relief=RAISED)
toolbar.pack(side=TOP, fill=X)
btn1 = Button(toolbar, relief=FLAT, compound= LEFT, text="",image=img1, command=submenu1)
btn1.pack(side=LEFT, padx=0, pady=0)
def submenu1():
# gif icon for submenu1:
imgvar1 = PhotoImage(file=r\path\.gif)
???
To make a button with a drop-down menu I suggest you use a Menubutton which was designed for that purpose — it's the part that stays on the screen all the time. After creating one you should create a Menu widget and configure the Menubutton to use it.
Once you have a Menu instance, you can populate it with different kinds of menu items, including ones made of images by using the Menu.insert_cascade() method. Also see this Menu item creation documentation which describes all the different kinds of menu items you can create and add to one.
Below is some sample code illustrating what I mean. For simplicity I used the same image twice — on the Menubutton and on one of the items on the drop-down menu that is displayed when it's clicked.
import tkinter as tk
from tkinter.constants import *
root = tk.Tk()
# Image Toolbar
img1 = tk.PhotoImage(file="8-ball.png")
# Toolbar
toolbar = tk.Frame(root, bd=1, relief=RAISED)
toolbar.pack(side=TOP, fill=X)
menubtn = tk.Menubutton(toolbar, relief=FLAT, compound=LEFT, text="", image=img1)
menubtn.pack(side=LEFT, padx=0, pady=0)
menu = tk.Menu(menubtn, tearoff=0)
menubtn.config(menu=menu)
menu.insert_command(0, label='Submit', command=lambda: print('Submit clicked'))
imgvar1 = tk.PhotoImage(file="8-ball.png")
menu.insert_cascade(1, image=imgvar1, command=lambda: print('Image pop out clicked'))
root.mainloop()

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.
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.

How do you make a drop down menu in Tkinter?

I have a quick question, how would you make a drop down menu in Tkinter like the one below:
This menu has a drop down option, how would you add a drop down in tkinter here is my code:
# Menu Bar
MenuBar = Menu(root)
root.config(menu=MenuBar)
MenuBar.config(bg="White", fg="Black", activebackground="Whitesmoke", activeforeground="Black", activeborderwidth=1, font=('Monaco', 11))
# Settings Option
SettingsOption = Menu(MenuBar, tearoff=False)
MenuBar.add_cascade(label="Settings", menu=SettingsOption)
SettingsOption.add_command(label="Help", command=None)
SettingsOption.add_command(label="Documentation", command=None)
So whenever I click Settings I should get a menu called help. Then when I hover over help I should get another dropdown menu called documentation. How would you do this in Python Tkinter?
You can use add_cascade() to add sub-menu:
import tkinter as tk
root = tk.Tk()
menubar = tk.Menu(root)
menubar.config(bg="white", fg="black", activebackground="whitesmoke", activeforeground="black", activeborderwidth=1, font="Monaco 11")
settings_menu = tk.Menu(menubar, tearoff=False)
help_menu = tk.Menu(settings_menu, tearoff=False)
help_menu.add_command(label="Documentation")
settings_menu.add_cascade(label="Help", menu=help_menu)
menubar.add_cascade(label="Settings", menu=settings_menu)
root.config(menu=menubar)
root.mainloop()

is it possible to have an optionmenu inside another optionmenu in tkinter python

I want some items from an optionmenu have further options to choose. I know about menu and menubutton widgets, but these dont help me. I have to use optionmenu
An optionmenu is literally just a menubutton and a menu, with a little bit of syntactic sugar.
Here's a simple example showing how you can have submenus on something that looks just like an optionmenu:
import tkinter as tk
root = tk.Tk()
var = tk.StringVar(value="one")
menubutton = tk.Menubutton(root, textvariable=var, indicatoron=True,
borderwidth=1, relief="raised", width=20)
main_menu = tk.Menu(menubutton, tearoff=False)
menubutton.configure(menu=main_menu)
for item in (("Numbers", "one", "two", "three"),
("Colors", "red", "green", "blue")
):
menu = tk.Menu(main_menu, tearoff=False)
main_menu.add_cascade(label=item[0], menu=menu)
for value in item[1:]:
menu.add_radiobutton(value=value, label=value, variable=var)
menubutton.pack(side="top", padx=20, pady=20)
root.mainloop()
If you want the user to be able to pick a different value from each sub-menu, you can just create a new StringVar for each menu. However, you'll have to write some code to update the label of the button yourself.

Labels and Buttons in Tkinter Menu

I created with Tkinter a Menubutton with an image. And I added in the Menubutton a label and two buttons.When I run my application, it works fine until I click two times the Menubutton. It starts to lag and it crashes.
Here is my code :
from Tkinter import *
from PIL import ImageTk
def donothing():
print("NOTHING")
root = Tk()
root.geometry("300x300")
Menu_Photo = Menubutton(root)
Photo = ImageTk.PhotoImage(file="image.png")
Menu_Photo.config(image=Photo)
Menu_Photo.image = Photo
Menu_Photo.menu = Menu(Menu_Photo, tearoff=0, relief=FLAT)
Menu_Photo["menu"] = Menu_Photo.menu
Menu_Photo.menu.label = Label(Menu_Photo.menu, text="Menu")
Menu_Photo.menu.Settings = Button(Menu_Photo.menu, text="Settings", relief=FLAT, command=donothing)
Menu_Photo.menu.Sign_Out = Button(Menu_Photo.menu, text="Sign Out", relief=FLAT, command=donothing)
Menu_Photo.menu.label.pack()
Menu_Photo.menu.Settings.pack(fill=BOTH)
Menu_Photo.menu.Sign_Out.pack(fill=BOTH)
Menu_Photo.pack(side=RIGHT, anchor=N)
root.mainloop()
I really don't understand what is the problem ?
This is what happens for me :
The menu opens and closes every second.

Categories

Resources