Jump to content

Python TKinter Buttons Making Buttons

Go to solution Solved by mail929,

You have several minor issues here:

  1. Global variables don't work in Python as you may expect. You must first call global [var name] when using one in a method.
  2. You forgot self. before master, easy mistake.
  3. You forgot to pass num to the string formatter.

Here's the working button() function:

def button(self):
    global num
    num = num + 1
    self.new_button = Button(self.master, text = "Button {}".format(num), command = self.button)
    self.new_button.pack()

 

from tkinter import Tk, Label, Button

num = 1

class MyFirstGUI:
    def __init__(self, master):
        self.master = master
        master.title("Button Simulator 2019")

        self.label = Label(master, text="Welcome to Button Simulator!")
        self.label.pack()

        self.first_button = Button(master, text="Button", command=self.button)
        self.first_button.pack()
        
    def button(self):
        num = num + 1
        self.new_button = Button(master, text = "Button {}".format(), command = self.button)
        self.new_button.pack()
        
root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()

I'm trying to learn TKinter for for my computer programming class and this is my code, I'm trying to make the button "first_button" create another button, and any button created will also be able to create another button. Each button will be labeled Button, Button 2, Button 3, etc. I keep getting errors, can anyone help? Thank you.

Link to comment
Share on other sites

Link to post
Share on other sites

You have several minor issues here:

  1. Global variables don't work in Python as you may expect. You must first call global [var name] when using one in a method.
  2. You forgot self. before master, easy mistake.
  3. You forgot to pass num to the string formatter.

Here's the working button() function:

def button(self):
    global num
    num = num + 1
    self.new_button = Button(self.master, text = "Button {}".format(num), command = self.button)
    self.new_button.pack()

 

Link to comment
Share on other sites

Link to post
Share on other sites

21 hours ago, mail929 said:

You have several minor issues here:

  1. Global variables don't work in Python as you may expect. You must first call global [var name] when using one in a method.
  2. You forgot self. before master, easy mistake.
  3. You forgot to pass num to the string formatter.

Here's the working button() function:


def button(self):
    global num
    num = num + 1
    self.new_button = Button(self.master, text = "Button {}".format(num), command = self.button)
    self.new_button.pack()

 

Sorry for the initial "solve" it solved the first error, but it still says : NameError: name 'num' is not defined

 

Edit: my fault, I forgot the initial num = 1 when I tried to test.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×