Python Forum
Class function does not create command button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class function does not create command button
#1
Hello Python Users:

The following code displays a command button "Click here to start". This command button is supposed to run the sa function. The sa function is supposed to display a new frame and command button and also print hello. It prints hello but does not display the command button or frame. Why does the command button not display?

import tkinter as tk
myfont = "Helvitica 30"
mybg = "lightskyblue"


class Main:

    def __init__(self, root):
        self.root = root
        self.root.geometry("500x500")
        self.createfstframe()

    def createfstframe(self):
        fstframe = tk.Frame(root)
        fstframe.pack(padx=100, pady=100)
        self.Hellobn=tk.Button(fstframe,text = "Hello", bg = mybg, font = myfont)
        self.Hellobn.pack(fill = tk.BOTH, expand = 1)
        self.Howyoubn = tk.Button(fstframe, text="How are you? ",bg = mybg ,font = myfont)
        self.Howyoubn.pack(fill = tk.BOTH, expand = 1)
        self.clickherebn = tk.Button(fstframe, text = "Click here to start", bg = "deepskyblue",font = myfont, command = self.sa)
        self.clickherebn.pack(fill = tk.BOTH, expand = 1)
        self.quitbn = tk.Button(fstframe,text = "Quit", font = myfont, bg = "red", command = fstframe.quit)
        self.quitbn.pack(fill = tk.BOTH, expand = 1)


    def sa(self):

        mnframe = tk.Frame(root)
        mnframe.pack(padx=100, pady=100)
        self.mybn = tk.Button(mnframe, text="mybutton", font=myfont, bg="red")
        self.mybn.pack(fill=tk.BOTH, expand=1)
        print("Hello")


root = tk.Tk()
b=Main(root)
root.mainloop()
Reply
#2
Why you think there isn't a button? Just because you can't see it? Change root's size to 1000x1000 and try again.
Reply
#3
Changing the geometry to 1000x1000 did work. My next goals was to destroy the first set of buttons. I was able to do this after making a few changes (as seen in the following code) after a bit of trial and error.

import tkinter as tk

myfont = "Helvitica 30"
mybg = "lightskyblue"


class Main:

    def __init__(self, root):
        self.root = root
        self.root.geometry("500x500")
        self.createfstframe()

    def createfstframe(self, bg= "green"):
        self.fstframe = tk.Frame(root)
        self.fstframe.pack(padx=100, pady=100)
        self.Hellobn = tk.Button(self.fstframe, text="Hello", bg=mybg, font=myfont)
        self.Hellobn.pack(fill=tk.BOTH, expand=1)
        self.Howyoubn = tk.Button(self.fstframe, text="How are you? ", bg=mybg, font=myfont)
        self.Howyoubn.pack(fill=tk.BOTH, expand=1)
        self.clickherebn = tk.Button(self.fstframe, text="Click here to start", bg="deepskyblue", font=myfont,
                                     command=self.sa)
        self.clickherebn.pack(fill=tk.BOTH, expand=1)
        self.quitbn = tk.Button(self.fstframe, text="Quit", font=myfont, bg="red", command=self.fstframe.quit)
        self.quitbn.pack(fill=tk.BOTH, expand=1)

    def sa(self):
        self.fstframe.destroy()
        mnframe = tk.Frame(root)
        mnframe.pack(padx=100, pady=100)
        self.mybn = tk.Button(mnframe, text="mybutton", font=myfont, bg="red")
        self.mybn.pack(fill=tk.BOTH, expand=1)
        print("Hello")


root = tk.Tk()
b = Main(root)
root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Use function from other class (Tkinter) zarize 8 499 Aug-17-2020, 09:47 AM
Last Post: zarize
  [Tkinter] Command button, then more command buttons Heyjoe 4 554 Aug-08-2020, 11:28 AM
Last Post: Yoriz
  [Tkinter] button command tkinter Heyjoe 6 1,321 Jul-30-2020, 07:06 PM
Last Post: deanhystad
  Button Command Heyjoe 4 418 Jul-20-2020, 01:45 AM
Last Post: Heyjoe
  [Tkinter] Creation of Buttons with Shared Command Inside Class MulliganAgain 1 261 Jul-08-2020, 06:22 PM
Last Post: Yoriz
  Tkinter:Unable to bind and unbind function with a button shallanq 2 732 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  [PyQt] call a function with parametrs from another class atlass218 3 598 Feb-29-2020, 11:00 AM
Last Post: atlass218
  tkinter button not accessing the command when clicked jhf2 1 626 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 1,291 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  Button in one class, methods in another one alan9979 4 720 Jul-11-2019, 01:57 AM
Last Post: joe_momma

Forum Jump:

User Panel Messages

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