我正在制作一个简单的联系人编译器,目前使用三个模块:一个用于接收信息的person类,一个用于允许用户提交信息的newContact Frame类,另一个用于首页Frame类,列出联系人的姓名,并为用户提供编辑列表的选项。
我遇到的问题是当我在首页类中调用newContact类的main函数时,它不会退出MainLoop。即使我销毁了newContact窗口,并且两个程序在没有相互调用时正常关闭。这是相关的代码:
import wx
import person
contacts = []
names = []
class newContact(wx.Frame):
def __init__(self, *args, **kwargs):
super(newContact, self).__init__(*args, **kwargs)
self.basicWindow()
self.Show()
def basicWindow(self):
panel = wx.Panel(self)
vertical = wx.BoxSizer(wx.VERTICAL)
heading = wx.StaticText(panel, label = 'Create New Contact')
vertical.Add(heading, flag = wx.LEFT | wx.TOP, border = 5)
nameBox = wx.BoxSizer(wx.HORIZONTAL)
nameText = wx.StaticText(panel, label = 'Name:')
nameBox.Add(nameText)
self.nameEntry = wx.TextCtrl(panel)
nameBox.Add(self.nameEntry, proportion = 1, flag = wx.LEFT, border = 50)
vertical.Add(nameBox, flag = wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT, border = 10)
phoneBox = wx.BoxSizer(wx.HORIZONTAL)
phoneText = wx.StaticText(panel, label = 'Number:')
phoneBox.Add(phoneText)
self.phoneEntry = wx.TextCtrl(panel)
phoneBox.Add(self.phoneEntry, proportion = 1, flag = wx.LEFT, border = 38)
vertical.Add(phoneBox, flag = wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT, border = 10)
mailBox = wx.BoxSizer(wx.HORIZONTAL)
mailText = wx.StaticText(panel, label = 'Email:')
mailBox.Add(mailText)
self.mailEntry = wx.TextCtrl(panel)
mailBox.Add(self.mailEntry, proportion = 1, flag = wx.LEFT, border = 53)
vertical.Add(mailBox, flag = wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT, border = 10)
submit = wx.Button(panel, label = 'Submit', size = (70,30))
vertical.Add(submit, flag = wx.ALIGN_RIGHT | wx.TOP | wx.RIGHT, border = 10)
self.Bind(wx.EVT_BUTTON, self.registerData)
panel.SetSizer(vertical)
def registerData(self, e):
name = self.nameEntry.GetValue()
name = name.title()
while True:
try:
number = int(self.phoneEntry.GetValue())
break
except ValueError:
number = 'a'
break
email = self.mailEntry.GetValue()
contact = person.Person(name, number, email)
names.append(name)
contacts.append(contact)
print('hello')
self.quit()
def quit(self):
self.Destroy()
def main():
app = wx.App()
newContact(None, title = 'New Contact', size = (405, 200), style= wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
app.MainLoop()
import wx
import newContact
class Front(wx.Frame):
def __init__(self, *args, **kwargs):
super(Front, self).__init__(*args, **kwargs)
self.interaction()
def interaction(self):
panel = wx.Panel(self)
wholeGrid = wx.BoxSizer(wx.VERTICAL)
mainBox = wx.BoxSizer(wx.HORIZONTAL)
title = wx.StaticText(panel, label = ' Contact List')
wholeGrid.Add(title, flag = wx.TOP | wx.LEFT, border = 10)
self.contactList = wx.ListBox(panel)
mainBox.Add(self.contactList, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 10)
buttonList = wx.BoxSizer(wx.VERTICAL)
new = wx.Button(panel, id = wx.ID_NEW, label = 'New', size = (100,30))
buttonList.Add(new)
edit = wx.Button(panel, id = wx.ID_EDIT, label = 'Edit', size = (100,30))
buttonList.Add(edit, flag = wx.TOP, border = 5)
delete = wx.Button(panel, id = wx.ID_DELETE, label = 'Delete', size = (100,30))
buttonList.Add(delete, flag = wx.TOP, border = 5)
mainBox.Add(buttonList, flag = wx.ALIGN_RIGHT | wx.TOP | wx.RIGHT, border = 10)
wholeGrid.Add(mainBox, proportion = 1, flag = wx.EXPAND)
panel.SetSizer(wholeGrid)
self.Bind(wx.EVT_BUTTON, self.createNew, id = wx.ID_NEW)
menu = wx.MenuBar()
fileMenu = wx.Menu()
fileMenu.Append(wx.ID_EXIT, 'Exit')
menu.Append(fileMenu, 'File')
self.SetMenuBar(menu)
self.Show(True)
def createNew(self, e):
newContact.main()
def main():
app = wx.App()
Front(None, title = "Contacts", size = (405,600), style = wx.SYSTEM_MENU | wx.CLOSE_BOX | wx.CAPTION)
app.MainLoop()
感谢您的帮助,我们也欢迎使用代码语法评论。
答案 0 :(得分:0)
在我看来,你只是关闭了newContact框架,而不是顶级前框架。您应该可以在newContact的quit方法中执行以下操作:
import wx
class newContact(wx.Frame):
def __init__(self, *args, **kwargs):
super(newContact, self).__init__(*args, **kwargs)
self.basicWindow()
self.Show()
def basicWindow(self):
panel = wx.Panel(self)
vertical = wx.BoxSizer(wx.VERTICAL)
heading = wx.StaticText(panel, label = 'Create New Contact')
vertical.Add(heading, flag = wx.LEFT | wx.TOP, border = 5)
nameBox = wx.BoxSizer(wx.HORIZONTAL)
nameText = wx.StaticText(panel, label = 'Name:')
nameBox.Add(nameText)
self.nameEntry = wx.TextCtrl(panel)
nameBox.Add(self.nameEntry, proportion = 1, flag = wx.LEFT, border = 50)
vertical.Add(nameBox, flag = wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT, border = 10)
phoneBox = wx.BoxSizer(wx.HORIZONTAL)
phoneText = wx.StaticText(panel, label = 'Number:')
phoneBox.Add(phoneText)
self.phoneEntry = wx.TextCtrl(panel)
phoneBox.Add(self.phoneEntry, proportion = 1, flag = wx.LEFT, border = 38)
vertical.Add(phoneBox, flag = wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT, border = 10)
mailBox = wx.BoxSizer(wx.HORIZONTAL)
mailText = wx.StaticText(panel, label = 'Email:')
mailBox.Add(mailText)
self.mailEntry = wx.TextCtrl(panel)
mailBox.Add(self.mailEntry, proportion = 1, flag = wx.LEFT, border = 53)
vertical.Add(mailBox, flag = wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT, border = 10)
submit = wx.Button(panel, label = 'Submit', size = (70,30))
vertical.Add(submit, flag = wx.ALIGN_RIGHT | wx.TOP | wx.RIGHT, border = 10)
self.Bind(wx.EVT_BUTTON, self.registerData)
panel.SetSizer(vertical)
def registerData(self, e):
#name = self.nameEntry.GetValue()
#name = name.title()
#while True:
#try:
#number = int(self.phoneEntry.GetValue())
#break
#except ValueError:
#number = 'a'
#break
#email = self.mailEntry.GetValue()
#contact = person.Person(name, number, email)
#names.append(name)
#contacts.append(contact)
self.quit()
def quit(self):
parent = self.GetParent()
parent.Close()
class Front(wx.Frame):
def __init__(self, *args, **kwargs):
super(Front, self).__init__(*args, **kwargs)
self.interaction()
def interaction(self):
panel = wx.Panel(self)
wholeGrid = wx.BoxSizer(wx.VERTICAL)
mainBox = wx.BoxSizer(wx.HORIZONTAL)
title = wx.StaticText(panel, label = ' Contact List')
wholeGrid.Add(title, flag = wx.TOP | wx.LEFT, border = 10)
self.contactList = wx.ListBox(panel)
mainBox.Add(self.contactList, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 10)
buttonList = wx.BoxSizer(wx.VERTICAL)
new = wx.Button(panel, id = wx.ID_NEW, label = 'New', size = (100,30))
buttonList.Add(new)
edit = wx.Button(panel, id = wx.ID_EDIT, label = 'Edit', size = (100,30))
buttonList.Add(edit, flag = wx.TOP, border = 5)
delete = wx.Button(panel, id = wx.ID_DELETE, label = 'Delete', size = (100,30))
buttonList.Add(delete, flag = wx.TOP, border = 5)
mainBox.Add(buttonList, flag = wx.ALIGN_RIGHT | wx.TOP | wx.RIGHT, border = 10)
wholeGrid.Add(mainBox, proportion = 1, flag = wx.EXPAND)
panel.SetSizer(wholeGrid)
self.Bind(wx.EVT_BUTTON, self.createNew, id = wx.ID_NEW)
menu = wx.MenuBar()
fileMenu = wx.Menu()
fileMenu.Append(wx.ID_EXIT, 'Exit')
menu.Append(fileMenu, 'File')
self.SetMenuBar(menu)
self.Show(True)
def createNew(self, e):
other_frame = newContact(self)
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App()
Front(None, title = "Contacts",
size = (405,600), style = wx.SYSTEM_MENU | wx.CLOSE_BOX | wx.CAPTION)
app.MainLoop()
稍微更改了代码,使其无需保存两个文件即可运行。您不应该像在 createNew 方法中那样直接调用类,所以我更改了...还要注意,无论如何都没有 main 方法。 / p>