wxpython单击按钮关闭父级
wxpython close parent on click button
我整个上午都在寻找最简单的东西,但我要么没有找到正确的地方,要么不理解我正在阅读的答案,因为到目前为止我尝试过的一切都失败了.
我正在创建一个带有标签、文本输入框和保存按钮的对话框。用户在框中输入文本,点击保存,文本被保存到一个变量中,对话框应该关闭。我不能让最后一点发生。我最接近的是破坏按钮和灰色背景,但保留面板本身完好无损。我正在寻找某种 KillParent 解决方案...我的主要问题是,当我单击按钮时,我需要发生两件不同的事情(保存和退出),所以当我完成变量保存后调用点击功能时我不再控制杀死主window。我知道这是非常基础的东西,但我就是想不通。
class ShowConnString(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.saveButton =wx.Button(self, label="Save", pos=(360, 50))
self.lblname = wx.StaticText(self, label="ConnectionString:", pos=(20,20))
self.editname = wx.TextCtrl(self, value="server='(local)', database='Audit', uid='sa', pwd='_PWD4sa_'", pos=(125, 18), size=(600,-1))
self.saveButton.Bind(wx.EVT_BUTTON, self.SaveConnString)
def SaveConnString(self, event):
self.editname.SelectAll()
self.connstringtext = self.editname.GetStringSelection()
print (self.connstringtext)
看来您正在尝试 class 子 wx.TextEntryDialog
但使用 wx.Panel
。
您可能会发现直接使用 wx.TextEntryDialog
更简单,即
import wx
app = wx.App()
dlg = wx.TextEntryDialog(None,"Connection String:","Save","server='(local)', database='Audit', uid='sa', pwd='_PWD4sa_'")
dlg.SetSize((600,180))
if dlg.ShowModal() == wx.ID_OK:
text = dlg.GetValue()
print text
dlg.Destroy()
它有点乱,但我现在没有时间清理它,它应该在 showconnstring class 中使用一个 sizer。
import wx
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "Dialog Test",size=(500,400))
self.panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
self.log = wx.TextCtrl(self.panel, wx.ID_ANY, size=(400,300),style = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL)
self.button = wx.Button(self.panel, label="Click me")
sizer.Add(self.log, 0, wx.EXPAND | wx.ALL, 10)
sizer.Add(self.button, 0, wx.EXPAND | wx.ALL, 10)
self.panel.SetSizer(sizer)
self.Bind(wx.EVT_BUTTON, self.OnButton)
def OnButton(self,event):
dlg = ShowConnString(parent = self.panel)
dlg.ShowModal()
if dlg.result_name:
self.log.AppendText("Name: "+dlg.result_name+"\n")
self.log.AppendText("Spin: "+str(dlg.result_spin)+"\n")
self.log.AppendText("Choice: "+str(dlg.result_choice)+"\n")
else:
self.log.AppendText("No selection made\n")
dlg.Destroy()
class ShowConnString(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent, wx.ID_ANY, "Save", size= (650,220))
self.panel = wx.Panel(self,wx.ID_ANY)
self.lblname = wx.StaticText(self.panel, label="Connection", pos=(20,20))
self.editname = wx.TextCtrl(self.panel, value="server=127.0.0.1", pos=(110,20), size=(500,-1))
self.lbl_1 = wx.StaticText(self.panel, wx.ID_ANY, ("Spin Control"), pos=(20,60))
self.spin = wx.SpinCtrl(self.panel, wx.ID_ANY, "", min=1, max=10, pos=(110,60))
self.lbl_2 = wx.StaticText(self.panel, wx.ID_ANY, ("Choice"),pos=(20,100))
self.choice = wx.Choice(self.panel, wx.ID_ANY, choices=[("Choice 1"), ("Choice 2"), ("Choice 3")], pos=(110,100))
self.saveButton =wx.Button(self.panel, label="Save", pos=(110,160))
self.closeButton =wx.Button(self.panel, 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.spin.SetValue(0)
self.choice.SetSelection(0)
self.Show()
def OnQuit(self, event):
self.result_name = None
self.Destroy()
def SaveConnString(self, event):
self.result_name = self.editname.GetValue()
self.result_spin = self.spin.GetValue()
self.result_choice = self.choice.GetSelection()
self.Destroy()
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()
我整个上午都在寻找最简单的东西,但我要么没有找到正确的地方,要么不理解我正在阅读的答案,因为到目前为止我尝试过的一切都失败了.
我正在创建一个带有标签、文本输入框和保存按钮的对话框。用户在框中输入文本,点击保存,文本被保存到一个变量中,对话框应该关闭。我不能让最后一点发生。我最接近的是破坏按钮和灰色背景,但保留面板本身完好无损。我正在寻找某种 KillParent 解决方案...我的主要问题是,当我单击按钮时,我需要发生两件不同的事情(保存和退出),所以当我完成变量保存后调用点击功能时我不再控制杀死主window。我知道这是非常基础的东西,但我就是想不通。
class ShowConnString(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.saveButton =wx.Button(self, label="Save", pos=(360, 50))
self.lblname = wx.StaticText(self, label="ConnectionString:", pos=(20,20))
self.editname = wx.TextCtrl(self, value="server='(local)', database='Audit', uid='sa', pwd='_PWD4sa_'", pos=(125, 18), size=(600,-1))
self.saveButton.Bind(wx.EVT_BUTTON, self.SaveConnString)
def SaveConnString(self, event):
self.editname.SelectAll()
self.connstringtext = self.editname.GetStringSelection()
print (self.connstringtext)
看来您正在尝试 class 子 wx.TextEntryDialog
但使用 wx.Panel
。
您可能会发现直接使用 wx.TextEntryDialog
更简单,即
import wx
app = wx.App()
dlg = wx.TextEntryDialog(None,"Connection String:","Save","server='(local)', database='Audit', uid='sa', pwd='_PWD4sa_'")
dlg.SetSize((600,180))
if dlg.ShowModal() == wx.ID_OK:
text = dlg.GetValue()
print text
dlg.Destroy()
它有点乱,但我现在没有时间清理它,它应该在 showconnstring class 中使用一个 sizer。
import wx
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "Dialog Test",size=(500,400))
self.panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
self.log = wx.TextCtrl(self.panel, wx.ID_ANY, size=(400,300),style = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL)
self.button = wx.Button(self.panel, label="Click me")
sizer.Add(self.log, 0, wx.EXPAND | wx.ALL, 10)
sizer.Add(self.button, 0, wx.EXPAND | wx.ALL, 10)
self.panel.SetSizer(sizer)
self.Bind(wx.EVT_BUTTON, self.OnButton)
def OnButton(self,event):
dlg = ShowConnString(parent = self.panel)
dlg.ShowModal()
if dlg.result_name:
self.log.AppendText("Name: "+dlg.result_name+"\n")
self.log.AppendText("Spin: "+str(dlg.result_spin)+"\n")
self.log.AppendText("Choice: "+str(dlg.result_choice)+"\n")
else:
self.log.AppendText("No selection made\n")
dlg.Destroy()
class ShowConnString(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent, wx.ID_ANY, "Save", size= (650,220))
self.panel = wx.Panel(self,wx.ID_ANY)
self.lblname = wx.StaticText(self.panel, label="Connection", pos=(20,20))
self.editname = wx.TextCtrl(self.panel, value="server=127.0.0.1", pos=(110,20), size=(500,-1))
self.lbl_1 = wx.StaticText(self.panel, wx.ID_ANY, ("Spin Control"), pos=(20,60))
self.spin = wx.SpinCtrl(self.panel, wx.ID_ANY, "", min=1, max=10, pos=(110,60))
self.lbl_2 = wx.StaticText(self.panel, wx.ID_ANY, ("Choice"),pos=(20,100))
self.choice = wx.Choice(self.panel, wx.ID_ANY, choices=[("Choice 1"), ("Choice 2"), ("Choice 3")], pos=(110,100))
self.saveButton =wx.Button(self.panel, label="Save", pos=(110,160))
self.closeButton =wx.Button(self.panel, 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.spin.SetValue(0)
self.choice.SetSelection(0)
self.Show()
def OnQuit(self, event):
self.result_name = None
self.Destroy()
def SaveConnString(self, event):
self.result_name = self.editname.GetValue()
self.result_spin = self.spin.GetValue()
self.result_choice = self.choice.GetSelection()
self.Destroy()
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()