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


Messages In This Thread
Class function does not create command button - by Heyjoe - Aug-22-2020, 02:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to create a GUI in Python which would accept a bash command max22 1 689 Nov-13-2023, 04:40 AM
Last Post: deanhystad
  [Kivy] Create a function to store text fields and drop downs selection in KivyMD floxia 0 1,660 Dec-18-2022, 04:34 AM
Last Post: floxia
Bug [Tkinter] CanĀ“t create a class for frames ThomasFab 5 2,009 Sep-28-2022, 08:44 PM
Last Post: deanhystad
  [Tkinter] Button 'command' Argument Confusion gw1500se 11 5,861 Nov-11-2021, 08:45 PM
Last Post: menator01
  Creating a function interrupt button tkinter AnotherSam 2 5,526 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,008 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  [Kivy] Acces atributes from a button defined in a class inside the .kv Tomli 2 2,087 Jun-10-2021, 01:05 AM
Last Post: Tomli
  [Tkinter] Modify Class on Button Click KDog 4 3,950 May-11-2021, 08:43 PM
Last Post: KDog
  Get name of command button Heyjoe 3 2,297 Dec-10-2020, 04:30 AM
Last Post: deanhystad
  [Tkinter] Use function from other class (Tkinter) zarize 8 4,801 Aug-17-2020, 09:47 AM
Last Post: zarize

Forum Jump:

User Panel Messages

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