NameError: name 'root' is not defined trying to click on menubar - python

I will try to be concrete, When I tried to click on the 'New' option menubar it is give me an error saying that the root it is not defined, any help why it is happening that?:
file.py", line 6, in newfile
root.filename = filedialog.askopenfilename(initialdir = "/Libraries/Document",title = "Select file",filetypes = (("lre
files",".lre"),("all files",".*"))) NameError: name 'root' is not
defined
I have two file in one I have this code file.py:
from tkinter import filedialog
from tkinter import *
def newfile():
print('hello')
root.filename = filedialog.askopenfilename(initialdir = "/Libraries/Document",title = "Select file",filetypes = (("lre files","*.lre"),("all files","*.*")))
print (root.filename)
And part of the other is this menu:
from tkinter import filedialog
from tkinter import *
import library.file as New
def donothing():
filewin = Toplevel(root)
button = Button(filewin, text="Do nothing button")
button.pack()
root = Tk()
root.state("zoomed")
root.title("Interactive Toys")
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=New.newfile)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu = menubar)
root.mainloop()

Related

Using same dataframe name in multiple functions in python

I have two excel files to import; file1.xls and file2.xls. I have define two separate function to import and third function to merge these files. However, merge is not happening. The error is showing
NameError: Name 'File1' is not defined.
I am using below code. Please help.
from tkinter import messagebox, filedialog, Frame, Button, Tk, Menu
import pandas as pd
def importivl():
global File1
LI_Filepath = filedialog.askopenfilename()
File1 = pd.read_excel(LI_Filepath)
messagebox.showinfo("File1", "File Imported Successfully")
def importcatfile():
global File2
Cat_Filepath = filedialog.askopenfilename()
File2 = pd.read_excel(Cat_Filepath)
messagebox.showinfo("File2", "File Imported Successfully")
def mergeivlcat():
File1 = pd.merge(File1, File2, on = "ID", how = "left")
def phase_one():
frame = Frame(root)
frame.pack()
import_LI_File = Button(root, text = "Import File1", command = importivl).pack()
import_Category_File = Button(root, text = "Import File2", command = importcatfile).pack()
mergeivlcat()
root = Tk()
root.geometry("600x400")
menubar = Menu(root)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label = "Phase One", command = phase_one)
filemenu.add_separator()
filemenu.add_command(label = "Exit", command = root.destroy)
menubar.add_cascade(label = "File", menu = filemenu)
root.config(menu = menubar)
root.mainloop()

menubar not defined in python

i am trying to make a program using the tkinter library in python but it gives error showing that ---
NameError: name 'menubar' is not defined
import tkinter
import sys
def hey():
print("hello")
def myNew():
mlabel = Label(root,text="yo").pack()
root = tkinter.Tk()
root.title("Wizelane")
root.geometry('400x80+350+340')
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="New",command=myNew)
label = tkinter.Label(root,text="say hello")
label.pack()
hello = tkinter.Button(root,text="hello",command=hey)
hello.pack()
root.mainloop()
You are missing some important parts here.
You need to configure the menu first and you also need to add the cascade label.
Take a look at this code.
import tkinter
def hey():
print("hello")
def myNew():
# you forgot to use tkinter.Label here.
mlabel = tkinter.Label(root, text="yo").pack()
root = tkinter.Tk()
root.title("Wizelane")
root.geometry('400x80+350+340')
my_menu = tkinter.Menu(root)
# configure root to use my_menu widget.
root.config(menu = my_menu)
# create a menu widget to place on the menubar
file_menu = tkinter.Menu(my_menu, tearoff=0)
# add the File cascade option for drop down use
my_menu.add_cascade(label = "File", menu = file_menu)
# then add the command you want to add as a File option.
file_menu.add_command(label="New", command = myNew)
label = tkinter.Label(root, text="say hello")
label.pack()
hello = tkinter.Button(root, text="hello", command = hey)
hello.pack()
root.mainloop()

NameError: name 'tkFileDialog' is not defined

I'm trying to use Tkinter and get the user to choose a certain file. My code looks like this (I'm just starting out with Tkinter)
from Tkinter import *
from tkFileDialog import *
root = Tk()
root.wm_title("Pages to PDF")
root.wm_iconbitmap('icon.ico')
w = Label(root, text="Please choose a .pages file to convert.")
y = tkFileDialog.askopenfilename(parent=root)
y.pack()
w.pack()
root.mainloop()
When I run the program, I get an error that says:
NameError: name 'tkFileDialog' is not defined
I've tried it with a few configurations I found online. None of them have worked; but this is the same basic error every time. How can I fix this?
You are importing everything from tkFileDialog module, so you don't need to write a module-name prefixed tkFileDialog.askopenfilename(), just askopenfilename(), like:
from Tkinter import *
from tkFileDialog import *
root = Tk()
root.wm_title("Pages to PDF")
w = Label(root, text="Please choose a .pages file to convert.")
fileName = askopenfilename(parent=root)
w.pack()
root.mainloop()
Try this:
from Tkinter import *
import tkFileDialog
root = Tk()
root.wm_title("Pages to PDF")
root.wm_iconbitmap('icon.ico')
w = Label(root, text="Please choose a .pages file to convert.")
y = tkFileDialog.askopenfilename(parent=root)
y.pack()
w.pack()
root.mainloop()
Seems a space name problem.
Try this:
try:
import Tkinter as tk
import tkFileDialog as fd
except:
import tkinter as tk
from tkinter import filedialog as fd
def NewFile():
print("New File!")
def OpenFile():
name = fd.askopenfilename()
print(name)
def About():
print("This is a simple example of a menu")
class myGUI:
def __init__(self, root):
self.root = root
self.canvas = tk.Canvas(self.root,
borderwidth=1,
relief="sunken")
self.canvas.pack( fill=tk.BOTH, expand=tk.YES)
self.menu = tk.Menu(self.root)
self.root.config(menu=self.menu)
self.helpmenu = tk.Menu(self.menu)
self.filemenu = tk.Menu( self.menu )
self.menu.add_cascade(label="File", menu=self.filemenu)
self.filemenu.add_command(label="New", command=NewFile)
self.filemenu.add_command(label="Open...", command=OpenFile)
self.filemenu.add_separator()
self.filemenu.add_command(label="Exit", command=root.destroy)
root = tk.Tk()
root.title('appName')
myGUI(root)
root.mainloop()

new window in python 3 and tkinter by clicking on button

This is program which I made for now, but I have problem...
how can I make when I click on button1 that then opens new window
import sys
from tkinter import *
import tkinter as tk
def mhello1():
mlabel = Label(mGui, text='A1').pack()
def mhello2():
mlabel = Label(mGui, text='A2').pack()
def mhello3():
mlabel = Label(mGui, text='A3').pack()
def mhello4():
mlabel
return
def mAbout():
messagebox.showinfo(title="About",message="program")
return
def mQuit():
mExit = messagebox.askyesno(title="Quit",message="y/n")
if mExit > 0:
mGui.destroy()
return
mGui = Tk()
mGui.geometry('450x450+200+200')
mGui.title('program')
mGui.configure(bg='gray')
mlabel = Label(text='option:',fg='red',bg = 'blue').pack()
mbutton1 = Button(mGui,text ='Button1',command = mhello1, height=5, width=20).pack()
mbutton2 = Button(mGui,text ='Button2',command = mhello2, height=5, width=20).pack()
mbutton3 = Button(mGui,text ='Button3',command = mhello3, height=5, width=20).pack()
mbutton4 = Button(mGui,text ='Button4',command = mhello4, height=5, width=20).pack()
mlabel2 = Label(text='activity:',fg='red',bg = 'blue').pack()
menubar=Menu(mGui)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label="qwer")
filemenu.add_command(label="quit",command = mQuit)
menubar.add_cascade(label="more options",menu=filemenu)
helpmenu = Menu(menubar, tearoff = 0)
helpmenu.add_command(label="Help Docs")
helpmenu.add_command(label="About", command = mAbout)
menubar.add_cascade(label="help",menu=helpmenu)
mGui.config(menu=menubar)
mGui.mainloop()
I try this program but it doesn't work:
Python 3 and tkinter opening new window by clicking the button
is there a way that I don't use tkinter toplevel?
Tnx a lot :)
Since you should create only one root window, you have to use a Toplevel to open a new one.
def mhello1():
toplevel = Toplevel()
toplevel.title('Another window')
toplevel.focus_set()
if you wanna use messagebox use those line below
from tkinter import *
from tkinter import messagebox

Tkinter dropdown Menu with keyboard shortcuts?

I would like to have a Dropdown Menu in Tkinter, that includes the shortcut key associated with this command. Is this possible?
How would I also add the underline under a certain character, to allow for Alt-F-S (File->Save)?
import tkinter as tk
import sys
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
menubar = tk.Menu(self)
fileMenu = tk.Menu(menubar, tearoff=False)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
fileMenu.add_command(label="Exit", underline=1,
command=quit, accelerator="Ctrl+Q")
self.config(menu=menubar)
self.bind_all("<Control-q>", self.quit)
def quit(self, event):
print("quitting...")
sys.exit(0)
if __name__ == "__main__":
app = App()
app.mainloop()
Maybe
from tkinter import *
import tkinter.filedialog as filed
root = Tk()
root.title("My Python Tkinter Application")
root.minsize(800,600)
def openfile():
fn = filed.askopenfilename(filetypes=[("Text Files","*.txt")], title="Open File")
f = open(fn, "r").read()
print(f)
def init():
menu = Menu(root)
filemenu = Menu(menu)
filemenu.add_command(label="Open (⌘O)", command=openfile)
menu.add_cascade(label="File", menu=filemenu)
root.config(menu=menu)
def key():
print("Key Pressed: "+repr(event.char))
root.bind("<Key>", key)
In this code press ctrl+e to exit the programmme
from tkinter import *
class MainApp:
def __init__(self, root):
self.root = root
self.menubar = Menu(self.root)
self.fileMenu = Menu(self.menubar, tearoff=False)
self.menubar.add_cascade(label="File", menu=self.fileMenu)
self.fileMenu.add_command(label="Exit", accelerator="Ctrl+E")
self.root.config(menu=self.menubar)
self.root.bind_all("<Control-e>", lambda event: self.root.quit())
self.root.mainloop()
if __name__ == "__main__":
MainApp(Tk())

Categories

Resources