나는 대화 기반 프로그램을 만들기 위해 wxPython을 사용하는 법을 배우고 있습니다. 나는 모두 Python IDLE
및 Apatana Studio 3
코드 위에 게재wxPython을 사용하여 대화 상자를 만드는 방법은 무엇입니까?
import wx
#---------------------------------------------------------------------------
class TestPanel(wx.Panel):
def __init__(self, parent, log):
self.log = log
wx.Panel.__init__(self, parent, -1)
b = wx.Button(self, -1, "Create and Show a DirDialog", (50,50))
self.Bind(wx.EVT_BUTTON, self.OnButton, b)
def OnButton(self, evt):
# In this case we include a "New directory" button.
dlg = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE
#| wx.DD_DIR_MUST_EXIST
#| wx.DD_CHANGE_DIR
)
# If the user selects OK, then we process the dialog's data.
# This is done by getting the path data from the dialog - BEFORE
# we destroy it.
if dlg.ShowModal() == wx.ID_OK:
self.log.WriteText('You selected: %s\n' % dlg.GetPath())
# Only destroy a dialog after you're done with it.
dlg.Destroy()
#---------------------------------------------------------------------------
def runTest(frame, nb, log):
win = TestPanel(nb, log)
return win
#---------------------------------------------------------------------------
overview = """\
This class represents the directory chooser dialog. It is used when all you
need from the user is the name of a directory. Data is retrieved via utility
methods; see the <code>DirDialog</code> documentation for specifics.
"""
if __name__ == '__main__':
import sys,os
import run
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
:
나는 (단지 wxPython에 데모에서 복사) 다음 코드를 시도했다. 여기에 내가 가진 것이있다.Python IDLE
에서
, 내가있어 :
IDLE Subprocess: no IP port passed in sys.argv.
그리고 Apatana Studio 3
에서
Traceback (most recent call last):
File "C:\Users\User\My Documents\Aptana Studio 3 Workspace\Test Dialogue\main.py", line 61, in import run ImportError: No module named run
는 내가 무슨 생각을 알고있다? 고마워. :)
안녕하세요, Ben. 아주 상세한 답변을 해주셔서 감사드립니다. 정말 도움이됩니다. 추가 질문 : 클래스'TestPanel '의 생성자에있는'log'은 무엇입니까? 코드를 실행하면 디렉토리를 선택한 후 오류가 발생합니다 :'Traceback (가장 최근 호출 마지막) : OnButton의 25 번째 줄 "P :/Project Backups/Python Practice/Examples/Demo_Panel2.py"파일 self.log.WriteText ('% dlg.GetPath())을 선택했습니다. AttributeError :'int '객체에'WriteText '속성이 없습니다. 나는이 오류가'log'를 가리키고 있다고 생각한다. 고맙습니다. – ChangeMyName
이 코드는 DirDialog의 wxpython 데모 코드에서 복사되며 로그는 데모에서 출력을 표시하는 데 사용됩니다. 로그에 대한 참조는 주석으로 처리 할 수 있습니다. – Yoriz
우수한 sleuthing! run.py 모듈에는 사용자가 선택한 것을 보여주기위한 Log 클래스가 포함되어 있습니다. Yoriz가 말했듯이 로그에 대한 참조를 삭제할 수 있습니다. 위의 예제 코드에 로그 클래스를 추가하여 해당 코드가 어떻게 적용되는지 확인할 수 있도록했습니다. 그것은 run.py에서 복사되었지만 클래스를 "새로운 스타일"클래스가 아닌 이전 스타일로 변경했습니다 (http://stackoverflow.com/questions/54867/old-style-and-new-style-classes- in-python) TestPanel의 줄을 self.log = log에서 self.log = Log()로 변경했습니다. – Ben