wxPython: displaying multiple widgets in same frame

I would like to be able to display Notebook and a TxtCtrl wx widgets in a single frame. Below is an example adapted from the wxpython wiki; is it possible to change their layout (maybe with something like wx.SplitterWindow) to display the text box below the Notebook in the same frame?

import wx
import wx.lib.sheet as sheet

class MySheet(sheet.CSheet):
    def __init__(self, parent):
        sheet.CSheet.__init__(self, parent)

        self.SetLabelBackgroundColour('#CCFF66')
        self.SetNumberRows(50)
        self.SetNumberCols(50)


class Notebook(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(600, 600))
        menubar = wx.MenuBar()
        file = wx.Menu()
        file.Append(101, 'Quit', '' )
        menubar.Append(file, "&File")
        self.SetMenuBar(menubar)
        wx.EVT_MENU(self, 101, self.OnQuit)
        nb = wx.Notebook(self, -1, style=wx.NB_BOTTOM)
        self.sheet1 = MySheet(nb)
        self.sheet2 = MySheet(nb)
        self.sheet3 = MySheet(nb)
        nb.AddPage(self.sheet1, "Sheet1")
        nb.AddPage(self.sheet2, "Sheet2")
        nb.AddPage(self.sheet3, "Sheet3")
        self.sheet1.SetFocus()
        self.StatusBar()

    def StatusBar(self):
        self.statusbar = self.CreateStatusBar()

    def OnQuit(self, event):
        self.Close()


class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(450, 400))
        self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)
        self.Center()

class MyApp(wx.App):
    def OnInit(self):
        frame = Notebook(None, -1, 'notebook.py')
        frame.Show(True)
        frame.Center()
        frame2 = MyFrame(None, -1, '')
        frame2.Show(True)
        self.SetTopWindow(frame2)
        return True


app = MyApp(0)
app.MainLoop()


Asked by: Fiona381 | Posted: 01-10-2021






Answer 1

Making two widgets appear on the same frame is easy, actually. You should use sizers to accomplish this.

In your example, you can change your Notebook class implementation to something like this:

class Notebook(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(600, 600))
        menubar = wx.MenuBar()
        file = wx.Menu()
        file.Append(101, 'Quit', '' )
        menubar.Append(file, "&File")
        self.SetMenuBar(menubar)
        wx.EVT_MENU(self, 101, self.OnQuit)
        nb = wx.Notebook(self, -1, style=wx.NB_BOTTOM)
        self.sheet1 = MySheet(nb)
        self.sheet2 = MySheet(nb)
        self.sheet3 = MySheet(nb)
        nb.AddPage(self.sheet1, "Sheet1")
        nb.AddPage(self.sheet2, "Sheet2")
        nb.AddPage(self.sheet3, "Sheet3")
        self.sheet1.SetFocus()
        self.StatusBar()
        # new code begins here:
        # add your text ctrl:
        self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)
        # create a new sizer for both controls:
        sizer = wx.BoxSizer(wx.VERTICAL)
        # add notebook first, with size factor 2:
        sizer.Add(nb, 2)
        # then text, size factor 1, maximized
        sizer.Add(self.text, 1, wx.EXPAND)
        # assign the sizer to Frame:
        self.SetSizerAndFit(sizer)

Only the __init__ method is changed. Note that you can manipulate the proportions between the notebook and text control by changing the second argument of the Add method.

You can learn more about sizers from the official Sizer overview article.

Answered by: Carina830 | Posted: 02-11-2021



Answer 2

You can use a splitter, yes.

Also, it makes sense to create a Panel, place your widgets in it (with sizers), and add this panel to the Frame.

Answered by: Ada409 | Posted: 02-11-2021



Similar questions

python - wxPython: program runs but not displaying

Before I made some changes to the following program, everything went fine: Program before modification: #! /usr/bin/env python """ A bare-minimum wxPython program """ import wx class MyApp(wx.App): def OnInit(self): return True class MyFrame(wx.Frame): def __init__(self, parent, title): wx.Frame.__init__(self, parent, title=title) if __name__ == '__main__': app = ...


python - Is there a known Win32 Tkinter bug with respect to displaying photos on a canvas?

I'm noticing a pretty strange bug with tkinter, and I am wondering if it's because there's something in how the python interacts with the tcl, at least in Win32. Here I have a super simple program that displays a gif image. It works perfectly. from Tkinter import * canvas = Canvas(width=300, height=300, bg='white') canvas.pack() photo=PhotoImage(file=sys.argv[1]) canvas.create_image(0, 0, imag...


python - Displaying X and y axis in pylab

Is there a way in pylab to display the X and Y axis? I know using grid() will display them, but it comes with numerous other lines all given the same emphasis.


python - How to prevent Satchmo forms from displaying asterisk after required fields?

I'm customizing my Satchmo store forms and have an icon that appears before any required fields. The problem is, Satchmo seems to want to render a text asterisk after the required fields. I'm using field.label to get this label, should I be using something else? EDIT: All my form templates are hard coded. I have an inclusion tag that takes a field and wraps it in a standard field te...


python - Displaying integers in a wxpython listctrl

I have a wxPython ListCtrl with five columns. Four of these hold strings, the last one has integer values. I have been storing these as strings (i.e. '4', '17', etc.). However, now that I have added a ColumnSorterMixin to let me sort specific columns in the list, I'm finding, of course, that the integer column is being sorted lexically rather than numerically. Is there a simple way of fixing this?


python - Displaying the Length of Individual Sequences in File

I have a file that contains two sequences. I have a program that could read all sequences, combine them together, and display the length of both sequences together. Now I want to display the length individually. The two sequences are separated by the symbol >. Example: SEQ1 >ATGGGACTAGCAGT SEQ2 >AGGATGATGAGTGA Program: #!usr/bin/python impor...


python - PyQt: Displaying QTextEdits over the window

I want to display some QTextEdits over my main window at arbitrary locations. Below is my first attempt. It doesn't quite work. If I create the text edits before I show the window, the text edits appear, but if I create them after I have shown the window they don't appear. What's up with that? How can I get the ones created later to show up? import sys, random from PyQt4 import QtGui, QtCore app = Qt...


Python XPath Result displaying only []

Hey I have just started to use Python recently and I want to use it with a bit of xPath, the thing is when I print the result of the query I only get [] and I don't know why =S import libxml2, urllib doc = libxml2.parseDoc(urllib.urlopen("http://www.domain.com/").read()) result = doc.xpathEval("//th//td[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]//a") if result != []: print result el...


python - Django, displaying a view in an another view?

I would like to know if I can display a view inside another view with django. This is what I tried to do: def displayRow(request, row_id): row = Event.objects.get(pk=row_id) return render_to_response('row.html', {'row': row}) def listEventsSummary(request): listEventsSummary = Event.objects.all().order_by('-id')[:20] response = '' for event in listEventsSummary: response...


python - Why is pdb displaying "*** Blank or comment" when I try to set a Break?

I'm working with my Django app. For some reason an element of a list is being assigned incorrectly. I'm trying to set a break where I think the error is occurring. ( line 20 ) I'm invoking pdb with this line of code: import pdb; pdb.set_trace() However, inside the code, I can't seem to set a Break. (Pdb) b 20 *** Blank or comment (Pdb) break 20 ***...


c++ - Displaying a cvMatrix containing complex numbers (CV_64FC2)

I'm new to OpenCV, and I would like to compare the results of a python program with my calculations in OpenCV. My matrix contains complex numbers since its the result of a cvDFT. Python handles complex numbers well and displays it with scientific notation. My C++ program is not effective when trying to use std::cout. I tried to store my numbers array in a std::complex[] instead of a double[] but it is not compiling...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top