Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [WxPython] Adding a Window to a Button wxPython
#1
Hey Guys, I'm new to Python Programming and have a major doubt about adding a window to a button inside the GUI. Could any of you guide me how and where I can add a window to a button? I'm a little unsure about the coding aspect as well. Here is the Current Code I'm using:
import wx
 
########################################################################
class MyForm(wx.Frame):
 
    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Audiometer")
        panel = wx.Panel(self, wx.ID_ANY,size=(1000,600))
        self.SetBackgroundColour((255,255,255))#2nd input change - ShanksD
        self.Centre()#Change -ShanksD
        self.Show()#Change -ShanksD

        
        #self.buttonOne.SetPosition((20, 20))
        #self.buttonTwo.SetPosition((40, 40))
        #self.buttonThree.SetPosition((60, 60))
        #self.buttonFour.SetPosition((80, 80))
        #self.buttonFive.SetPosition((100, 100))
        #self.buttonSix.SetPosition((120, 120))
        #self.buttonSeven.SetPosition((140, 140))
 
        sizer = wx.BoxSizer(wx.VERTICAL)
        buttonOne = wx.Button(panel, label="Frequency", name="Frequency")
        buttonTwo = wx.Button(panel, label="Volume", name="Volume")
        buttonThree = wx.Button(panel, label="Pulse Tone", name="Pulse Tone")
        buttonFour = wx.Button(panel, label="Mask", name="Mask")
        buttonFive = wx.Button(panel, label="Air Bone", name="Air Bone")
        buttonSix = wx.Button(panel, label="Test Select", name="Test Select")
        buttonSeven = wx.Button(panel, label="INT", name="INT")
                              
        buttons = [buttonOne, buttonTwo, buttonThree,buttonFour, buttonFive,buttonSix,buttonSeven]
 
        for button in buttons:
            self.buildButtons(button, sizer)
 
        panel.SetSizer(sizer)
 
    #----------------------------------------------------------------------
    def buildButtons(self, btn, sizer):
        """"""
        btn.Bind(wx.EVT_BUTTON, self.onButton)
        sizer.Add(btn, 10, wx.ALL,37 )#Do not Change the VA=alue
 
    #----------------------------------------------------------------------
    def onButton(self, event):
        """
        This method is fired when its corresponding button is pressed
        """
        button = event.GetEventObject()
        print ("The button you pressed was labeled  ") + button.GetLabel()
        print ("The button's name is ") + button.GetName()
 
        button_id = event.GetId()
        button_by_id = self.FindWindowById(button_id)
        print ("The button you pressed was labeled: ") + button_by_id.GetLabel()
        print ("The button's name is ") + button_by_id.GetName()
 
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()
Quote
#2
Not sure, but in my understanding of what you are asking, for when button pressed, new tkinter window will popup, right? If it is that, the code is below.

from tkinter import *

root = Tk()

def new_winF(): # new window definition
    newwin = Toplevel(root)
    display = Label(newwin, text="Humm, see a new window !")
    display.pack()    

button1 =Button(root, text ="open new window", command =new_winF) #command linked
button1.pack()

root.mainloop()
Quote
#3
francisco, poster is looking for wxpython solution
francisco_neves2020 likes this post
Quote
#4
(Apr-23-2019, 11:52 AM)Larz60+ Wrote: francisco, poster is looking for wxpython solution
sorry, i have to pay more attention. i went searching to give him the answer he needs. I miss that.
Quote
#5
I'm not sure what type of window your looking for but I've added the creation of a dialog window to the following code.
import wx
  
########################################################################
class MyForm(wx.Frame):
  
    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Audiometer")
        panel = wx.Panel(self, wx.ID_ANY,size=(1000,600))
        self.SetBackgroundColour((255,255,255))#2nd input change - ShanksD
        self.Centre()#Change -ShanksD
        self.Show()#Change -ShanksD
 
         
        #self.buttonOne.SetPosition((20, 20))
        #self.buttonTwo.SetPosition((40, 40))
        #self.buttonThree.SetPosition((60, 60))
        #self.buttonFour.SetPosition((80, 80))
        #self.buttonFive.SetPosition((100, 100))
        #self.buttonSix.SetPosition((120, 120))
        #self.buttonSeven.SetPosition((140, 140))
  
        sizer = wx.BoxSizer(wx.VERTICAL)
        buttonOne = wx.Button(panel, label="Frequency", name="Frequency")
        buttonTwo = wx.Button(panel, label="Volume", name="Volume")
        buttonThree = wx.Button(panel, label="Pulse Tone", name="Pulse Tone")
        buttonFour = wx.Button(panel, label="Mask", name="Mask")
        buttonFive = wx.Button(panel, label="Air Bone", name="Air Bone")
        buttonSix = wx.Button(panel, label="Test Select", name="Test Select")
        buttonSeven = wx.Button(panel, label="INT", name="INT")
                               
        buttons = [buttonOne, buttonTwo, buttonThree,buttonFour, buttonFive,buttonSix,buttonSeven]
  
        for button in buttons:
            self.buildButtons(button, sizer)
  
        panel.SetSizer(sizer)
        panel.Layout() # Added to make the sizer layout it's controls
  
    #----------------------------------------------------------------------
    def buildButtons(self, btn, sizer):
        """"""
        btn.Bind(wx.EVT_BUTTON, self.onButton)
        sizer.Add(btn, 10, wx.ALL,37 )#Do not Change the VA=alue
  
    #----------------------------------------------------------------------
    def onButton(self, event):
        """
        This method is fired when its corresponding button is pressed
        """
        button = event.GetEventObject()
        print (f"The button you pressed was labeled {button.GetLabel()}") # fixed string formatting
        print (f"The button's name is {button.GetName()}") # fixed string formatting
  
        button_id = event.GetId()
        button_by_id = self.FindWindowById(button_id)
        print (f"The button you pressed was labeled: {button_by_id.GetLabel()}") # fixed string formatting
        print (f"The button's name is {button_by_id.GetName()}") # fixed string formatting
        
        dlg = MyDialog(self, title=button.GetLabel())
        dlg.ShowModal()
#         dlg.Destroy()
  
#----------------------------------------------------------------------

class MyDialog(wx.Dialog):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.SetBackgroundColour((255,255,255))
        title = kwargs['title']
        sizer = wx.BoxSizer(wx.VERTICAL)
        label = wx.StaticText(self, -1, "This is a wx.Dialog")
        label2 = wx.StaticText(
            self, -1, f"This was created by clicking button {title}")
        sizer.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(label2, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        btnsizer = self.CreateSeparatedButtonSizer(wx.CLOSE)
        sizer.Add(btnsizer, 0, wx.ALIGN_CENTER|wx.ALL|wx.GROW, 5)
        self.SetSizer(sizer)
        sizer.Fit(self)


# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  tkinter window and turtle window error 1885 3 127 Nov-02-2019, 12:18 PM
Last Post: 1885
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 344 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 500 May-12-2019, 08:17 PM
Last Post: joe_momma
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 645 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  Adding Progressbar to button aniyanetworks 9 2,023 Feb-07-2019, 11:12 AM
Last Post: Larz60+
  [PyQt] Resize button with window resize FesterJester 2 1,715 Dec-03-2018, 12:02 AM
Last Post: FesterJester
  [Tkinter] Spawn sub-window with button press malonn 3 1,236 Oct-28-2018, 02:56 PM
Last Post: malonn
  tkinter button acts normal ONLY after clicking off the window justin_m 8 1,378 Apr-24-2018, 06:14 AM
Last Post: Larz60+
  set button background with image of window background gray 3 6,927 Oct-18-2017, 08:22 AM
Last Post: gray
  [Tkinter] Selected radio button in push button in Tkinter prashantfunde91 1 6,361 Jun-22-2017, 05:27 PM
Last Post: DeaD_EyE

Forum Jump:


Users browsing this thread: 1 Guest(s)