Python Forum
[Tkinter] Button error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Button error
#1
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.
Reply
#2
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.
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,012 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [PyQt] No reaction and no error message when clicking button Atalanttore 4 4,812 Nov-23-2018, 01:48 PM
Last Post: Atalanttore
  Error while changing button relief from raised to sunken on left click mgtheboss 1 5,351 Jan-09-2018, 06:05 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020