gpt4 book ai didi

python - 在按钮上单击打开 wxpython TextEntryDialog 并从用户那里获取多个输入

转载 作者:太空宇宙 更新时间:2023-11-04 05:04:50 25 4
gpt4 key购买 nike

我想在用户单击按钮时打开一个 TextEntryDialog。因此,如果我在父框架中有一个按钮,我将以这种方式绑定(bind):

      self.Bind(wx.EVT_BUTTON, self.OnAddNew, self.add_new_btn)

现在我必须在用户单击按钮 add_new 时打开一个 TextEntryDialog。我想让 textentrydialog 像这样 Python, Using wxPython to get multiple input from user

我该怎么做?我是否需要将该代码粘贴到`def OnAddNew(self, event):这是我的代码的 pastebin 链接:https://pastebin.com/UEYscgFa我在一个函数中创建了类,那么可以用那种方式吗?

最佳答案

不!
GetData 本身就是一个类。
该代码已经为您提供了方法。MyFrame 毫无意义,只是为了创建一个独立的工作示例。

def OnButton(self,event):
dlg = GetData(parent = self.panel)
dlg.ShowModal()
if dlg.result_name:
self.log.AppendText("Name: "+dlg.result_name+"\n")
self.log.AppendText("Surname: "+dlg.result_surname+"\n")
self.log.AppendText("Nickname: "+dlg.result_nickname+"\n")
else:
self.log.AppendText("No Input found\n")
dlg.Destroy()

编辑:我不明白我的评论中的说明在哪里躲避了你,但为了我的罪过,这里是你的代码,按照评论中的方式清理和编辑。

import sqlite3
import wx
import os

class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title, size=(1000,800))
self.inter_list = list()
self.plot_list = list()
self.InitUI()
self.Layout()
self.Centre()
self.Show()

def InitUI(self):
self.p = wx.Panel(self)
bs = wx.BoxSizer(wx.VERTICAL)
gs = wx.GridSizer(10, 18, 5, 5)
bs.Add(gs, 1, wx.EXPAND)
self.search_btn=wx.Button(self.p,-1,"Search!")
self.search_btn.Bind(wx.EVT_BUTTON, self.OnSearch, self.search_btn)
bs.Add(self.search_btn,0,wx.ALIGN_CENTER)
self.p.SetSizer(bs)

def OnSearch(self, event):
dlg = GetData(parent = self.p)
dlg.ShowModal()
if dlg.result_name:
print "Name: "+dlg.result_name+"\n"
print "Surname: "+dlg.result_surname+"\n"
print "Nickname: "+dlg.result_nickname+"\n"
else:
print "No Input found\n"
dlg.Destroy()

class GetData(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent, wx.ID_ANY, "Name Input", size= (650,220))
self.p = wx.Panel(self,wx.ID_ANY)
self.lblname = wx.StaticText(self.p, label="Name", pos=(20,20))
self.name = wx.TextCtrl(self.p, value="", pos=(110,20), size=(500,-1))
self.lblsur = wx.StaticText(self.p, label="Surname", pos=(20,60))
self.surname = wx.TextCtrl(self.p, value="", pos=(110,60), size=(500,-1))
self.lblnick = wx.StaticText(self.p, label="Nickname", pos=(20,100))
self.nickname = wx.TextCtrl(self.p, value="", pos=(110,100), size=(500,-1))
self.saveButton =wx.Button(self.p, label="Save", pos=(110,160))
self.closeButton =wx.Button(self.p, label="Cancel", pos=(210,160))
self.saveButton.Bind(wx.EVT_BUTTON, self.SaveConnString)
self.closeButton.Bind(wx.EVT_BUTTON, self.OnQuit)
self.Bind(wx.EVT_CLOSE, self.OnQuit)
self.Show()

def OnQuit(self, event):
self.result_name = None
self.Destroy()

def SaveConnString(self, event):
self.result_name = self.name.GetValue()
self.result_surname = self.surname.GetValue()
self.result_nickname = self.nickname.GetValue()
self.Destroy()

app = wx.App()
Example(None, title = 'Raman Spectroscopy Database')
app.MainLoop()

关于python - 在按钮上单击打开 wxpython TextEntryDialog 并从用户那里获取多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44773414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com