I am trying to create a new frame in a wxpanel by clicking a button.
Below is my code.
It does not work.
Scroll bars do not show up.
Can anyone help me? Thanks!
(update: A button is added in the new window)
import wx
class ftest(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"test Panel", size=(800, 500), pos=(0,0))
self.MainPanel = wx.Panel(self, wx.ID_ANY)
self.btn1 = wx.Button(self.MainPanel, pos=(50,100), label="New Frame")
self.btn1.Bind(wx.EVT_BUTTON, self.newFrame)
def newFrame(self, event):
self.new_window = wx.Frame(self, title='frame2', size=(500, 500), pos=(800,0))
self.new_window.Show()
self.scroll = wx.ScrolledWindow(self.new_window, -1)
self.scroll.SetScrollbars(1, 1, 1600, 1400)
self.btn2 = wx.Button(self.new_window, pos=(50,100), label="button2")
if __name__ == "__main__":
app = wx.App(False)
frame = ftest()
frame.Show()
app.MainLoop()
,
(update 2)
based on Joran Beasley's code,
this code creates scroll bars, but the button2 is not shown.
and the text widget does not work properly when scrolled.
import wx
class ftest(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"test Panel", size=(800, 500), pos=(0,0))
self.MainPanel = wx.Panel(self, wx.ID_ANY)
self.btn1 = wx.Button(self.MainPanel, pos=(50,100), label="New Frame")
self.btn1.Bind(wx.EVT_BUTTON, self.newFrame)
def newFrame(self, event):
self.new_window = wx.Frame(self, title='frame2', pos=(800,0))
self.scroll = wx.ScrolledWindow(self.new_window, -1,size=(500,500))
self.scroll.SetScrollbars(1, 1, 1600, 1400)
self.new_window.Layout()
self.new_window.Fit()
self.new_window.Show()
self.btn2 = wx.Button(self.new_window, pos=(50,100), label="button2")
wx.StaticText(self.new_window, -1, 'test text', pos=(50, 200))
if __name__ == "__main__":
app = wx.App(False)
frame = ftest()
frame.Show()
app.MainLoop()
,
(update 3)
I found my mistake. The widgets should be on the scroll object, not on the frame object.
Layout() and Fit() are not required.
so correct code is
import wx
class ftest(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"test Panel", size=(800, 500), pos=(0,0))
self.MainPanel = wx.Panel(self, wx.ID_ANY)
self.btn1 = wx.Button(self.MainPanel, pos=(50,100), label="New Frame")
self.btn1.Bind(wx.EVT_BUTTON, self.newFrame)
def newFrame(self, event):
self.new_window = wx.Frame(self, title='frame2', pos=(800,0),size=(500,500))
self.scroll = wx.ScrolledWindow(self.new_window, -1)
self.scroll.SetScrollbars(1, 1, 1600, 1400)
#self.new_window.Layout()
#self.new_window.Fit()
self.new_window.Show()
self.btn2 = wx.Button(self.scroll, pos=(50,100), label="button2")
wx.StaticText(self.scroll, -1, 'test text', pos=(50, 200))
if __name__ == "__main__":
app = wx.App(False)
frame = ftest()
frame.Show()
app.MainLoop()
def newFrame(self, event):
self.new_window = wx.Frame(self, title='frame2', size=(500, 500), pos=(800,0))
self.scroll = wx.ScrolledWindow(self.new_window, -1)
self.scroll.SetScrollbars(1, 1, 1600, 1400)
self.new_window.Layout()
self.new_window.Fit()
self.new_window.Show()
you need to layout the new window ... since you clearly want it to fill the 500,500 area you will need to use sizers
def newFrame(self, event):
self.new_window = wx.Frame(self, title='frame2', size=(500, 500), pos=(800,0))
sz = wx.BoxSizer()
sz.SetMinSize((500,500)) #force minimum size
self.scroll = wx.ScrolledWindow(self.new_window, -1)
sz.Add(self.scroll,1,wx.EXPAND)
self.scroll.SetScrollbars(1, 1, 1600, 1400)
self.new_window.SetSizer(sz)
self.new_window.Layout()
self.new_window.Fit()
self.new_window.Show()
or just force the size of the contained scrollwindow (which is what you normally do for scrolled windows)
def newFrame(self, event):
self.new_window = wx.Frame(self, title='frame2', pos=(800,0))
self.scroll = wx.ScrolledWindow(self.new_window, -1,size=(500,500))
self.scroll.SetScrollbars(1, 1, 1600, 1400)
self.new_window.Layout()
self.new_window.Fit()
self.new_window.Show()
Related
I set the size in my panel, but it does not take action when I run the program. It just pops up small and I have to manually resize.
I have tried setting/initializing a frame, but when I do that it blocks any input. So I can no longer use my program. I have also tried numerous times to set it within the panel settings.
class CipherTexter(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, size=(5000, 5000))
#wx.Frame.__init__(self, parent, size=(5000, 5000))
self.cipherText = wx.StaticText(self, label="Cipher Texter ", pos=(20, 30))
…
app = wx.App(False)
frame = wx.Frame(None, wx.ID_ANY, "The SS Cipher")
panel = CipherTexter(frame)
frame.Show()
app.MainLoop()
When I first open the application, I get this. I would like to be able to see the whole thing instead of having to manually resize it.
Try putting the "panel" within the "frame".
Here is a mock up of what is, I suppose, your required starting point.
import wx
class CipherTexter(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(1000, 600))
self.panel = wx.Panel(self)
cipherText = wx.StaticText(self.panel, label="Cipher Texter ", pos=(20, 30))
encryptorText = wx.StaticText(self.panel, label="Encryptor ", pos=(20, 70))
decryptorText = wx.StaticText(self.panel, label="Decryptor ", pos=(20, 100))
self.cipher = wx.TextCtrl(self.panel, -1, style=wx.TE_MULTILINE, size=(400,400), pos=(400, 30))
self.encryptor = wx.TextCtrl(self.panel, -1, size=(100,30), pos=(200, 70))
self.decryptor = wx.TextCtrl(self.panel, -1, size=(100,30), pos=(200, 100))
self.encrypt = wx.Button(self.panel, -1, "Encrypt", pos=(20, 140))
self.decrypt = wx.Button(self.panel, -1, "Decrypt", pos=(20, 180))
self.panel.SetBackgroundColour('white')
self.encrypt.Bind(wx.EVT_BUTTON, self.encryptNow)
self.decrypt.Bind(wx.EVT_BUTTON, self.decryptNow)
self.Show()
def encryptNow(self, event):
print("Encrypting")
cipher = self.cipher.GetValue()
encryptor = self.encryptor.GetValue()
print(cipher)
print("with ", encryptor)
def decryptNow(self, event):
print("De-Encrypting")
cipher = self.cipher.GetValue()
decryptor = self.decryptor.GetValue()
print(cipher)
print("with ", decryptor)
app = wx.App(False)
frame = CipherTexter(None, "The SS Cipher")
app.MainLoop()
I wrote a code to receive some input data from user using both CheckBox and TextCtrl. The problem is when I marked the checkbox and textctrl appears, it accept to receive input data, but won't replace with the default one!
import wx
class mainClass(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Interface', size=(500, 250))
self.panel = wx.Panel(self)
self.checkReplaceJob = wx.CheckBox(self.panel, -1, "Replace data", (35, 60), (235, 20))
self.Bind(wx.EVT_CHECKBOX, self.replaceJob, self.checkReplaceJob)
self.RunBut = wx.Button(self.panel, -1, "Run", pos=(150, 150))
self.Bind(wx.EVT_BUTTON, self.RunClick, self.RunBut)
self.RunBut.SetDefault()
self.CloseBut = wx.Button(self.panel, -1, "Close", pos=(250, 150))
self.Bind(wx.EVT_BUTTON, self.CloseClick, self.CloseBut)
def CloseClick(self, event):
self.Close()
def replaceJob(self, event):
if(self.checkReplaceJob.IsChecked()):
self.repJobRetName()
self.btn = wx.Button(self.panel, wx.ID_ANY, "&Help", pos=(345, 82))
self.Bind(wx.EVT_BUTTON, self.HelpJobName, self.btn)
def repJobRetName(self):
self.label = wx.StaticText(self.panel, -1, label = "New name:", pos=(165,87))
self.entry = wx.TextCtrl(self.panel, -1, value = u"Task-1", pos=(230, 84))
repJobName = self.entry.GetValue()
return repJobName
def HelpJobName(self, event):
help = 'Write out new name.'
wx.MessageBox(help, "Help")
def RunClick(self, event):
if(self.checkReplaceJob.IsChecked()):
replaceName = self.repJobRetName()
wx.MessageBox('The new name is: ' + replaceName, "Info")
#############=======================
if __name__ == "__main__":
app = wx.App(False)
mainClass().Show()
app.MainLoop()
Let me make sure I understand correctly. You want the text control to have a default value when it is created, but you want that value to disappear when you actually select the control. Is that correct? If so, then you just need to add a binding to wx.EVT_SET_FOCUS and do a little work when the widget gets focus. Here's an example:
import wx
class mainClass(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Interface', size=(500, 250))
self.panel = wx.Panel(self)
self.checkReplaceJob = wx.CheckBox(self.panel, -1, "Replace data", (35, 60), (235, 20))
self.Bind(wx.EVT_CHECKBOX, self.replaceJob, self.checkReplaceJob)
self.RunBut = wx.Button(self.panel, -1, "Run", pos=(150, 150))
self.Bind(wx.EVT_BUTTON, self.RunClick, self.RunBut)
self.RunBut.SetDefault()
self.CloseBut = wx.Button(self.panel, -1, "Close", pos=(250, 150))
self.Bind(wx.EVT_BUTTON, self.CloseClick, self.CloseBut)
def CloseClick(self, event):
self.Close()
def replaceJob(self, event):
if(self.checkReplaceJob.IsChecked()):
self.repJobRetName()
self.btn = wx.Button(self.panel, wx.ID_ANY, "&Help", pos=(345, 82))
self.Bind(wx.EVT_BUTTON, self.HelpJobName, self.btn)
def repJobRetName(self):
self.label = wx.StaticText(self.panel, -1, label = "New name:", pos=(165,87))
self.entry = wx.TextCtrl(self.panel, -1, value = u"Task-1", pos=(230, 84))
self.entry.Bind(wx.EVT_SET_FOCUS, self.onFocus)
repJobName = self.entry.GetValue()
return repJobName
def onFocus(self, event):
current_value = self.entry.GetValue()
if current_value == "Task-1":
self.entry.SetValue("")
def HelpJobName(self, event):
help = 'Write out new name.'
wx.MessageBox(help, "Help")
def RunClick(self, event):
if(self.checkReplaceJob.IsChecked()):
replaceName = self.repJobRetName()
wx.MessageBox('The new name is: ' + replaceName, "Info")
#############=======================
if __name__ == "__main__":
app = wx.App(False)
mainClass().Show()
app.MainLoop()
In progress of making an wxpython assignment for school, I stumbled on something I can't seem to figure out by myself.
The main idea of the assignment is to make a self-generating quiz. Now I made a frame were the questions should be in the future.
I need this frame to update itself with the button for the next question (middle one), so the next question is showing when clicking the button.
Before actually doing that I tried testing it with a random number generator. But the update button doesn't seem to update to a new frame with a new number (looking the same, only the number changing). I know I'm missing something, but I don't know where to start.
Here is my code:
import wx
import random
class welkom(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, "Aminozuurtoets V.1.0", size=(900,600))
self.mainFrame = mainFrame
top_panel = wx.Panel(self)
w_tekst = wx.StaticText(top_panel, -1, "Welkom bij de aminozuurtoets",(325,50), (100, -1), wx.ALIGN_CENTER)
w_font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
w_tekst.SetFont(w_font)
st_nr = wx.StaticText(top_panel, -1, 'Studentnummer' ,(100,150))
inp_st_nr = wx.TextCtrl(top_panel, -1, '', (300,150), size=(140,-1))
st_vr= wx.StaticText(top_panel, -1, 'Student voornaam' ,(100,200))
inp_st_vr = wx.TextCtrl(top_panel, -1, '', (300,200), size=(140,-1))
st_ach = wx.StaticText(top_panel, -1, 'Student achternaam' ,(100,250))
inp_st_ach = wx.TextCtrl(top_panel, -1, '', (300,250), size=(140,-1))
aan_vr = wx.StaticText(top_panel, -1, 'Aantal vragen' ,(100,300))
inp_aan_vr = wx.TextCtrl(top_panel, -1, '20', (300,300), size=(140,-1))
close_button = wx.Button(top_panel, label = "Stoppen", pos=(600, 400), size=(150, 200))
self.Bind(wx.EVT_BUTTON, self.closebutton, close_button)
go_button = wx.Button(top_panel, label = "Doorgaan", pos=(100, 400), size=(150, 200))
self.Bind(wx.EVT_BUTTON, self.buttonClick, go_button)
def closebutton(self, event):
self.Close(True)
def buttonClick(self, event):
self.Hide()
self.mainFrame(None, id = -1).Show()
class mainFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, "Aminozuurtoets V.1.0", size=(900,600))
top_panel = wx.Panel(self)
self.vraag = 1
m_tekst = wx.StaticText(top_panel, -1, "Vraag " + str(self.vraag),(400,50), (100, -1), wx.ALIGN_CENTER)
m_font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
m_tekst.SetFont(m_font)
cijfer = random.randint(1,100)
test2 = wx.StaticText(top_panel, -1, str(cijfer), (325,300))
res_but = wx.Button(top_panel, label = "Resultaten", pos=(650, 400), size=(150, 200))
ga_naar = wx.Button(top_panel, label = "Ga naar vraag", pos=(100, 400), size=(150, 200))
ga_button = wx.Button(top_panel, label = "Volgende vraag", pos=(380, 400), size=(150, 200))
self.Bind(wx.EVT_BUTTON, self.buttonClick1, ga_button)
def buttonClick1(self, event):
self.Update()
def closebutton(self, event):
self.Close(True)
app = wx.App()
frame = welkom(None, id = -1).Show()
app.MainLoop()
Just calling Update doesn't change nothing. I changed mainFrame class. (See comments that starts with ###)
class mainFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, "Aminozuurtoets V.1.0", size=(900,600))
top_panel = wx.Panel(self)
self.vraag = 1
m_tekst = wx.StaticText(top_panel, -1, "Vraag " + str(self.vraag),(400,50), (100, -1), wx.ALIGN_CENTER)
m_font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
m_tekst.SetFont(m_font)
cijfer = random.randint(1,100)
### Make an attribute to access from buttonClick1 method.
self.test2 = wx.StaticText(top_panel, -1, str(cijfer), (325,300))
res_but = wx.Button(top_panel, label = "Resultaten", pos=(650, 400), size=(150, 200))
ga_naar = wx.Button(top_panel, label = "Ga naar vraag", pos=(100, 400), size=(150, 200))
ga_button = wx.Button(top_panel, label = "Volgende vraag", pos=(380, 400), size=(150, 200))
self.Bind(wx.EVT_BUTTON, self.buttonClick1, ga_button)
def buttonClick1(self, event):
### Change label of static text.
self.test2.Label = str(random.randint(1,100))
def closebutton(self, event):
self.Close(True)
This looks simple but I have not found any documentation. Tried with &&, which does not work. Want a button like this:
Button1=wx.Button(self, 5, "abc&abc", (130, 230))
Works for me with this:
import wx
class MainWindow(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(200,-1))
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.buttons = [
wx.Button(self,-1,"Button &One"),
wx.Button(self,-1,"Button &&Two"),
]
for btn in self.buttons:
self.sizer.Add(btn,1,wx.EXPAND)
self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)
self.Show()
app = wx.App(False)
frame = MainWindow(None,"Hello Ampersand")
app.MainLoop()
Your code working MattH but my problem is actually i was showing button in click event of menubar at that time && not works..check out below code...what should be done in such a case
import wx
class MainWindow(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(699, 570))
fileMenu= wx.Menu()
folder=wx.MenuItem(fileMenu, 1, "&Start","Click here to start..")
fileMenu.AppendItem(folder)
about = wx.MenuItem(fileMenu, 2, '&About',"Test")
fileMenu.AppendItem(about)
quit = wx.MenuItem(fileMenu, 3, '&Quit',"Terminate the program")
fileMenu.AppendItem(quit)
menuBar = wx.MenuBar()
menuBar.Append(fileMenu,"&File")
self.Bind(wx.EVT_MENU, self.ShowButton, folder)
self.SetMenuBar(menuBar)
pndSummaryButton = wx.Button(self, 3, "Button &&Two", (130, 146))
pndSummaryButton.name="pndSummary"
self.printArea2 = wx.TextCtrl(self,pos = (290, 146), size = (255, 25),style = wx.TE_READONLY)
pndSummaryButton.SetFont(wx.Font(11, wx.SWISS, wx.NORMAL,wx.LIGHT))
self.printArea2.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL,wx.LIGHT))
pndSummaryButton.SetBackgroundColour(wx.Colour(153,0,0))
pndSummaryButton.SetForegroundColour(wx.Colour(255,255,255))
pndSummaryButton.SetSize(pndSummaryButton.GetBestSize())
self.Show()
def ShowButton(self,event):
pndSummaryButton = wx.Button(self, 3, "Button &&Two", (130, 176))
pndSummaryButton.name="pndSummary1"
self.printArea2 = wx.TextCtrl(self,pos = (290, 176), size = (255, 25),style = wx.TE_READONLY)
pndSummaryButton.SetFont(wx.Font(11, wx.SWISS, wx.NORMAL,wx.LIGHT))
self.printArea2.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL,wx.LIGHT))
pndSummaryButton.SetBackgroundColour(wx.Colour(153,0,0))
pndSummaryButton.SetForegroundColour(wx.Colour(255,255,255))
pndSummaryButton.SetSize(pndSummaryButton.GetBestSize())
app = wx.App(False)
frame = MainWindow(None,"Hello Ampersand")
app.MainLoop()
I'm trying to build a small application in wxPython (absolute beginner) in which I display a login box before showing the content. I created a frame, inside the frame a panel with a flexigrid to put the login form inside but it doesn't show. If I launch the application the login form is invisible. If I resize the application the login box shows. Any idea why? Here's my code so far:
import wx
class AP_App(wx.App):
def OnInit(self):
frame = AP_MainFrame("Test application", (0, 0), (650, 350))
frame.Show()
self.SetTopWindow(frame)
loginPanel = AP_LoginPanel(frame)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
return True
def OnCloseWindow(self, event):
self.Destroy()
class AP_MainFrame(wx.Frame):
def __init__(self, title, pos, size):
wx.Frame.__init__(self, None, -1, title, pos, size)
self.CreateStatusBar()
class AP_LoginPanel(wx.Panel):
def __init__(self, frame):
self.panel = wx.Panel(frame)
self.frame = frame
self.frame.SetStatusText("Authentification required!")
self.showLoginBox()
def showLoginBox(self): #Create the sizer
sizer = wx.FlexGridSizer(rows = 3, cols = 2, hgap = 5, vgap = 15)
# Username
self.txt_Username = wx.TextCtrl(self.panel, 1, size = (150, -1))
lbl_Username = wx.StaticText(self.panel, -1, "Username:")
sizer.Add(lbl_Username,0, wx.LEFT|wx.TOP| wx.RIGHT, 50)
sizer.Add(self.txt_Username,0, wx.TOP| wx.RIGHT, 50)
# Password
self.txt_Password = wx.TextCtrl(self.panel, 1, size=(150, -1), style=wx.TE_PASSWORD)
lbl_Password = wx.StaticText(self.panel, -1, "Password:")
sizer.Add(lbl_Password,0, wx.LEFT|wx.RIGHT, 50)
sizer.Add(self.txt_Password,0, wx.RIGHT, 50)
# Submit button
btn_Process = wx.Button(self.panel, -1, "&Login")
self.panel.Bind(wx.EVT_BUTTON, self.OnSubmit, btn_Process)
sizer.Add(btn_Process,0, wx.LEFT, 50)
self.panel.SetSizer(sizer)
def OnSubmit(self, event):
UserText = self.txt_Username.GetValue()
PasswordText = self.txt_Password.GetValue()
if __name__ == '__main__':
app = AP_App()
app.MainLoop()
I just discovered I'm calling frame.Show() too soon. :)