I have a text box in wxpython (see below) that stores the name of the value as a variable.
I am trying to do two things:
After the answer is entered, I want to display another question, and 开发者_开发知识库assign the new answer to another variable, using the same or an idential TextEntryDialog window.
Ideally, from a user standpoint, they just see a prompt, type an answer (or select from a list), and then after hitting OK, the prompt will change, and they will type in a new answer (which will be assigned to a new variable).
So why am I trying to do this? So that after the end of this Q & A session, I can write all of the variables to a database using pyodbc (which I dont need to know about right now).
So could you please tell me how I can automatically generate new prompts once an answer has been entered without closing the app and losing the variable data? And is there anyway to automatically backup this variable data while the user is answering in case the app crashes? My question list is about 250 questions long, and I dont want all those variables lost if my application crashes (which they tend to do)
Thanks!
import wx
class applicationName(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Title', size=(300,200))
#create panel and button
panel = wx.Panel(self)
test = wx.TextEntryDialog(None, "What's your name?", 'Title', 'Enter name')
if test.ShowModal() == wx.ID_OK:
apples = test.GetValue()
wx.StaticText(panel, -1, apples, (10,10))
if __name__ =='__main__':
app = wx.PySimpleApp()
frame = applicationName(parent=None, id=-1)
frame.Show()
app.MainLoop()
I don't recommend creating and destroying 250 dialogs like the other fellow did. I would probably create a list or dict at the beginning of my program that would get appended to whenever the user enters an answer. Also in that event handler, I would reset the StaticText control with a new question. You might need to refresh the screen if your questions vary a lot in length, but I think that would be a lot better than showing hundreds of dialogs in a row.
EDIT - Added some example code below:
import wx
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
self.answers = {}
self.questions = ["What is your age?", "What is your weight?",
"Which of the following computer languages is the best ever: C++, PHP, Fortran, COBOL, Python?"]
self.nextQuestion = 0
self.question = wx.StaticText(panel, label="What is your name?")
self.answer = wx.TextCtrl(panel, value="")
submitBtn = wx.Button(panel, label="Submit")
submitBtn.Bind(wx.EVT_BUTTON, self.onSubmit)
sizer = wx.BoxSizer(wx.VERTICAL)
self.panelSizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.question, 0, wx.ALL, 5)
sizer.Add(self.answer, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(submitBtn, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)
self.panelSizer.Add(panel, 1, wx.EXPAND)
self.SetSizer(self.panelSizer)
#----------------------------------------------------------------------
def onSubmit(self, event):
""""""
self.answers[self.question.GetLabel()] = self.answer.GetValue()
self.question.SetLabel(self.questions[self.nextQuestion])
self.answer.SetValue("")
self.nextQuestion += 1
print self.answers
self.panelSizer.Fit(self)
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
- Write a function that an entry dialog and displays and returns the value the user entered
- Put it in a for loop
You can even do something like:
answers = [getanswer(q) for q in questions]
getanswer
could look like:
def getanswer(q):
test = wx.TextEntryDialog(None, *q)
if test.ShowModal() == wx.ID_OK:
return test.GetValue() # returns None the user didn't select OK.
questions
can contain lists or tuples of the stuff you want to pass to the constructor of wx.TextEntryDialog
.
精彩评论