Using the menu in Python - The Advantages of Learning Python Programming for Beginners: A Comprehensive Guide

Latest

Dive into the world of Python programming with our comprehensive resources

Music

logo

Thursday, February 9, 2023

Using the menu in Python

 

In this article, I will make a simple example of using the menu in Python.  My menu code is like below

 


 

 

 

 

 

 

 

 

 

 

Figure 1

 


 

 

 

 

 

 

 

 

 

Figure 2

 

 

pyton_menu.py

 

from tkinter import *

from tkinter.filedialog import askopenfilename

import tkinter as tk

 

def NewFile():

    print("Hello World!")

    str1="Hello World!"

 

    lbl = tk.Label(root, text=str1, bg="deepskyblue", fg="white",font=('arial',12,'bold'))

    lbl.pack(fill=tk.X, padx=20,pady=10)

 

def OpenFile():

    name = askopenfilename()

    print(name)

 

    lbl2 = tk.Label(root, text=name, bg="red", fg="white",font=('arial',12,'bold'))

    lbl2.pack(fill=tk.X, padx=40,pady=20)

 

def About():

    print("Menu..bs")

 

    lbl3 = tk.Label(root, text="Menu..bs", bg="navy", fg="white",font=('arial',12,'bold'))

    lbl3.pack(fill=tk.X, padx=60,pady=30)

   

root = Tk()

root.title("menu..bs")

root.geometry("250x250")

 

menu = Menu(root)

 

root.config(menu=menu)

 

filemenu = Menu(menu)

menu.add_cascade(label="File", menu=filemenu)

filemenu.add_command(label="New", command=NewFile)

filemenu.add_command(label="Open...", command=OpenFile)

filemenu.add_separator()

filemenu.add_command(label="Exit", command=root.quit)

 

helpmenu = Menu(menu)

menu.add_cascade(label="Help", menu=helpmenu)

helpmenu.add_command(label="About...", command=About)

 

root.mainloop()

 

 

I wish you happy coding days until we meet in the next article.

No comments:

Post a Comment

Featured post

Python Introduction

What is Python? Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built ...