with wxPython v4.1.1, is there a function or recommended method to clear the value from a previously written wx.TextCtrl value?
While testing GetValue() I noticed that the previous value remains when a new value is written into the wx.StaticText area. I would like to remover/clean the old value out and write the new one.
Any help would be greatly appreciated.
Hi, mfones
The following codes equivalently work.
Which one you choose depends on your coding style or just a favor.
textctrl.SetValue("spam")
textctrl.Value = "spam"
Thanks komoto48g,
I changed course a little and used a NumCtrl. Which gave some unexpected results, not part of this question.
This question appears related to when I call my calculate function. The panel maintains the size of the parent at the time the function is called. The user enters the value and clicks the calculate button, the answer appears. But when I click on the corner of the window to resize the child panel displaying the answer will not resize.
Whereas before I hit the calculate button the panel can be resized to any shape you want.
Any thoughts would be appreciated.
Here’s the program.
class TabPanel(wx.Panel):
"""
This class is needed to add the tab panels below.
"""
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
class MainFrame(wx.Frame):
"""
Frame that holds all other widgets. The app and main if loop are in this
"""
def __init__(self):
"""
Constructor: This is where the frame, Panel, menuBar with the file
menu, and all the notebooks or tabs are.
"""
wx.Frame.__init__(self, None, wx.ID_ANY, "ATC Calculator",
size = (600, 400)
)
icon = wx.Icon('G:\Resources\Icons\wrench.ico', wx.BITMAP_TYPE_ANY)
self.SetIcon(icon)
panel = wx.Panel(self)
menuBar = wx.MenuBar()
fileMenu = wx.Menu()
menuBar.Append(fileMenu, "&File")
fileMenu.Append(wx.ID_ANY, "&Open", "Open File")
fileMenu.Append(wx.ID_ANY, "&New", "New File")
fileMenu.Append(wx.ID_ANY, "&Save", "Save File")
fileMenu.AppendSeparator() # Thin line separator
# File Menu Exit Binding.
fileExit = fileMenu.Append(wx.ID_ANY, "&Exit", "Exit File")
fileMenu.Bind(wx.EVT_MENU, self.onQuit, fileExit)
editMenu = wx.Menu()
menuBar.Append(editMenu, "&Edit") # Main header "Edit" added last
editMenu.Append(wx.ID_ANY, "&Copy", "Copy")
editMenu.Append(wx.ID_ANY, "&Cut", "Cut")
editMenu.Append(wx.ID_ANY, "Paste", "Paste")
editMenu.AppendSeparator()
editMenu.Append(wx.ID_ANY, "&Options...", "Display Options")
helpMenu = wx.Menu()
menuBar.Append(helpMenu, "&Help") # Main header "Edit" added last
helpMenu.Append(wx.ID_ANY, "&Copy", "Copy")
helpMenu.Append(wx.ID_ANY, "&Cut", "Cut")
helpMenu.Append(wx.ID_ANY, "Paste", "Paste")
# Separator
helpMenu.AppendSeparator()
# About pop up binding.
aboutModal = helpMenu.Append(wx.ID_ANY, "&About...", "About")
helpMenu.Bind(wx.EVT_MENU, self.onAbout, aboutModal)
# Attach Menubar to the Frame
self.SetMenuBar(menuBar)
# Adding the notebook "Tab" to the panel.
notebook = wx.Notebook(panel, style=wx.NB_TOP)
tabOne = TabPanel(notebook)
notebook.AddPage(tabOne, "ATC Calculations")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
panel.SetSizer(sizer)
self.Show()
# Add Some Static text to the tabPanel
custom = wx.StaticText(tabOne, -1, "Static Text on Notebook/Tab One", (10,10))
font = wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
custom.SetFont(font)
tabOne.SetBackgroundColour((240, 240, 240))
wx.StaticText(tabOne, -1, "TTC", (10, 40))
wx.StaticText(tabOne, -1, "ATC", (10, 100))
ttc_Num = self.numbox = NumCtrl(parent = tabOne,
#value = 1, # Default value 0
# pos = (10, 60), # wx.DefaultPostion
pos = (10, 60), # wx.DefaultPostion
style = TE_PROCESS_ENTER,
validator = wx.DefaultValidator,
name = "masked.number",
# integerWidth = 6,
fractionWidth = 0,
allowNone = False,
allowNegative = False,
useParensForNegatives = False,
groupDigits = False, # Comma applied to three digits.
groupChar = ',',
decimalChar = '.',
min = None,
max = None,
limited = False,
limitOnFieldChange = False,
selectOnEntry = True,
foregroundColour = "Black",
signedForegroundColour = "Red",
emptyBackgroundColour = "White",
validBackgroundColour = "White",
invalidBackgroundColour = "Yellow",
autoSize = False,
)
# Calculate Button.
self.Calculate = wx.Button(parent = tabOne, label = 'Calculate', pos = (10, 150))
self.Calculate.Bind(wx.EVT_BUTTON, self.onCalculate)
self.ClearBtn = wx.Button(parent = tabOne, label = 'Clear', pos = (100, 150))
self.ClearBtn.Bind(wx.EVT_BUTTON, self.onClear)
self.CreateStatusBar()
self.Layout()
self.Show()
def onCalculate(self, event):
ttc = self.numbox.GetValue()
atc = ttc * .5
print(atc)
wx.TextCtrl(self, 1, str(atc), pos = (20, 150))
return atc
def onClear(self, event):
self.Clear.Clear()
def onQuit(self, event):
self.Close()
def onAbout(self, event):
info = wx.adv.AboutDialogInfo()
info.Name = "ATC Calculator"
info.Version = " v1.0"
info.Copyright = "(c) 2021 by Mike Smith"
info.Description = wordwrap(
"The \"ATC Calculator\" is a software program that calulates the Available Transmission Capacity using TTC, TRM and the TRMCoef."
"\n\nUsers enter TTC, TRM and TRMCoef value and the program returns the ATC",
350, wx.ClientDC(self))
info.WebSite = ("http://en.wikipedia.org/wiki/Hello_world", "Hello World home page")
info.Developers = [ "Mike Smith",
"Bob Lawbla",
"Fred Flintstone" ]
licenseText = "blah " * 250 + "\n\n" + "yadda " * 100
# ClientDC
info.License = wordwrap(licenseText, 500, wx.ClientDC(self))
wx.adv.AboutBox(info)
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
# frame.CentreOnScreen() # Center the window too.
frame.Centre() # Center the window too.
frame.SetSizeHints(minW=600, minH=400, incW=-1, incH=-1)
app.MainLoop()
I think this part causes the problem.
The wx.TextCtrl
object is created each time the Calculate
button is pressed, but not destroyed. In addition, the object is created as the child of the Frame, not the front panel.