Python Forum

Full Version: Button error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Please help,

Why am I getting this error.

Main.py

from tkinter import *
import tkinter as tk
import tkinter.font as tkFont
import NewSetup


main = tk.Tk()

#variables
fontStyle = tkFont.Font(family="Lucida Grande", size=10)

#functions
def NewSetupTab():
    NewSetup.Setup()

main.title("Setup Selector")
main.geometry('600x500-375-150')

canvas = tk.Canvas(main, bg='white', width=600, height=600)
canvas.pack()

main.resizable(0,0)
main.overrideredirect(1)

my_menu = Menu(main)
main.config(menu=my_menu)
create_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="   +   ",menu=create_menu, font= "Courier 100")
create_menu.add_command(label="New Setup", command=NewSetupTab)



main.mainloop()
NewSetup.py

from tkinter import *
import tkinter as tk
import tkinter.font as tkFont



def Setup():
    

    # fontStyle1 = tkFont.Font(family="Lucida Grande", size=10)

    newsetup = tk.Tk()
    newsetup.overrideredirect(1)
    newsetup.geometry('900x700-275-60')
    canvas = tk.Canvas(newsetup, bg="light green", width=1000, height=800)
    canvas.pack()

    SetupTitle = tk.Label(newsetup, bg="light green", text= 'Name of Setup')
    canvas.create_window(150, 50, window=SetupTitle,)

    SetupEntry = tk.Entry (newsetup)
    canvas.create_window(150, 75, window=SetupEntry)
    

    def SetupValue():
        print(SetupEntry.get())
    button1 = tk.Button(text='Add', command=SetupValue)
    canvas.create_window(200, 75, window=button1)


    ProgramTitle = tk.Label(newsetup, bg="light green", text= 'Path of Programs to Open (With Quotes)')
    canvas.create_window(150, 125, window=ProgramTitle,)

    ProgramEntry = tk.Entry(newsetup)
    canvas.create_window(150, 150, window=ProgramEntry)

    newsetup.mainloop()
But when I run NewSetup.py on its own everything is fine.
In NewSetup.py the following button has no parent
button1 = tk.Button(text='Add', command=SetupValue)
add newsetup as the first passed argument
button1 = tk.Button(newsetup, text='Add', command=SetupValue)

Other notes
Remove the star imports from tkinter import * see Namespace flooding with * imports
Normal practice is to only have one instance of tk.Tk() and only one call to mainloop if you want a second window use tk.Toplevel.

https://www.python.org/dev/peps/pep-0008...dule-names Wrote:Package and Module Names
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.
(Jun-19-2021, 04:15 PM)Yoriz Wrote: [ -> ]In NewSetup.py the following button has no parent
button1 = tk.Button(text='Add', command=SetupValue)
add newsetup as the first passed argument
button1 = tk.Button(newsetup, text='Add', command=SetupValue)

Other notes
Remove the star imports from tkinter import * see Namespace flooding with * imports
Normal practice is to only have one instance of tk.Tk() and only one call to mainloop if you want a second window use tk.Toplevel.

https://www.python.org/dev/peps/pep-0008...dule-names Wrote:Package and Module Names
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

Thank you it worked