问题描述
你好。我正在寻找一种更好,更紧凑的编写代码的方式。它不一定是任何花哨的东西,只是没有
重复的东西最好只有一个返回语句。
def create_db_name(self):
dlg = wx.TextEntryDialog(self.frame,''输入数据库名称:' ',
''创建新数据库'')
如果dlg.ShowModal()== wx.ID_OK:
db_name = dlg。 GetValue()
dlg.Destroy()
返回db_name
else:
dlg.Destroy()
返回
一个问题是,如果取消按下,我什么都不能回来。
另一个问题是必须销毁对话框,所以必须在任何返回语句之前获得
。
谢谢。
这应该有效:
def create_db_name(self):
dlg = wx。 TextEntryDialog(self.frame,''输入数据库名称:'',
''创建新数据库'')
db_name =无
如果dlg.ShowModal()== wx.ID_OK:
db_name = dlg.GetValue()
dlg.Destroy()
返回db_name
-Farshid
我喜欢
如果dlg.ShowModal()== wx.ID_OK:
db_name = dlg.GetValue()
else:
db_name =无
dlg.Destroy()
返回db_name
优于
db_name =无
如果dlg.ShowModal()== wx.ID_OK:
db_name = dlg.GetValue()
dlg.Destroy()
返回db_name
但我想这是一个偏好问题。
谢谢大家!我没想到以这种方式将db_name设置为None!
Hi guys. I''m looking for a nicer, more compact way of writing this code.
It doesn''t have to be anything fancy, just something without the
duplication and preferably only one return statement.
def create_db_name(self):
dlg = wx.TextEntryDialog(self.frame, ''Enter a database name:'',
''Create New Database'')
if dlg.ShowModal() == wx.ID_OK:
db_name = dlg.GetValue()
dlg.Destroy()
return db_name
else:
dlg.Destroy()
return
One problem is that if "Cancel" is pressed, I can''t return anything.
Another problem is that the dialog must be destroyed, so that has to
come before any return statements.
Thanks.
This should work:
def create_db_name(self):
dlg = wx.TextEntryDialog(self.frame, ''Enter a database name:'',
''Create New Database'')
db_name = None
if dlg.ShowModal() == wx.ID_OK:
db_name = dlg.GetValue()
dlg.Destroy()
return db_name
-Farshid
I like
if dlg.ShowModal() == wx.ID_OK:
db_name = dlg.GetValue()
else:
db_name = None
dlg.Destroy()
return db_name
better than
db_name = None
if dlg.ShowModal() == wx.ID_OK:
db_name = dlg.GetValue()
dlg.Destroy()
return db_name
but I suppose it''s a matter of preference.
Thanks guys! I didn''t think about setting db_name to None in that way!
这篇关于更清洁的方式写这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!