Wxpython - 如何在不关闭应用程序的情况下生成一系列文本提示?

发布于 2024-11-14 03:15:00 字数 1032 浏览 3 评论 0原文

我在 wxpython 中有一个文本框(见下文),它将值的名称存储为变量。

我想做两件事:

输入答案后,我想显示另一个问题,并使用相同或相同的 TextEntryDialog 窗口将新答案分配给另一个变量。

理想情况下,从用户的角度来看,他们只是看到提示,输入答案(或从列表中选择),然后点击“确定”后,提示将发生变化,他们将输入一个新答案(该答案将被分配给新变量)。

那么我为什么要尝试这样做呢?所以在本次问答结束后在会话中,我可以使用 pyodbc 将所有变量写入数据库(我现在不需要知道)。

那么您能否告诉我如何在输入答案后自动生成新提示而不关闭应用程序并丢失变量数据?无论如何,在用户接听时是否可以自动备份这些可变数据,以防应用程序崩溃?我的问题列表大约有 250 个问题,如果我的应用程序崩溃(他们往往会这样做),我不希望所有这些变量丢失,

谢谢!

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 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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯 2024-11-21 03:15:00

我不建议像其他人那样创建和销毁 250 个对话框。我可能会在程序开始时创建一个列表或字典,每当用户输入答案时就会附加到该列表或字典中。同样在该事件处理程序中,我将使用新问题重置 StaticText 控件。如果您的问题长度变化很大,您可能需要刷新屏幕,但我认为这比连续显示数百个对话框要好得多。

编辑 - 在下面添加了一些示例代码:

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()

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()
高跟鞋的旋律 2024-11-21 03:15:00
  • 编写一个函数,输入对话框显示并返回用户输入的值
  • 将其放入 for 循环中

您甚至可以执行类似以下操作:

answers = [getanswer(q) for q in questions]

getanswer 可以如下所示:

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 可以包含要传递给 wx.TextEntryDialog 构造函数的内容的列表或元组。

  • 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文