Hi, id like to set values for a botton is wxpython by taking that value from a text file.

For this code

button = wx.Button(parent, label = “x”)

and for example on the 1st line of the text file id like to read x. Ive played about with this a bit and I believe I have to do something along the lines of

button = wx.Button(parent, label = “%s(x)”

but am unsure of the syntax or whether im even calling the information from the file right, can anyone help me out?

'''-->my_file.txt
x
a
b
'''

text_label = [f.strip() for f in open('my_file.txt')][0]
self.button = wx.Button(panel, label = '%s' % text_label)

Take x from my_file.txt and place it as label text for button.

Edited 6 Years Ago by snippsat: n/a

Lines read from a text file are strings, so even something as simple as this will work ...

# a simple wxPython template for testing widgets

'''-->my_file.txt
x
a
b
'''

import wx

app = wx.App(0)
frame = wx.Frame(None, wx.ID_ANY, size=(300, 200))
panel = wx.Panel(frame, wx.ID_ANY, size=(300, 200))


#text_label = [f.strip() for f in open('my_file.txt')][0]
#button = wx.Button(panel, label = '%s' % text_label)
text_label = open('my_file.txt').readlines()[0].strip()
button = wx.Button(panel, label=text_label)


frame.Show()
app.MainLoop()
This question has already been answered. Start a new discussion instead.