我一直在为一些工作创建一个python GUI。在Python知识方面,我会自我描述为新手。我正在使用wxPython和wxGlade来帮助进行GUI开发。
问题如下:
我有一个空的TextCtrl对象,旁边有一个Button。
按钮用于打开FileDialog,并使用所选文件位置的值填充或替换TextCtrl。我已经创建了按钮的功能来打开FileDialog,但我似乎无法弄清楚如何使用该结果值填充TextCtrl。
import wx
class frmCheckSubmital(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: frmCheckSubmitall.__init__
kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.rbxUtilitySelect = wx.RadioBox(self, wx.ID_ANY, "Utility", choices=["Stormwater", "Sewer", "Water"], majorDimension=1, style=wx.RA_SPECIFY_ROWS)
self.txtFeaturesPath = wx.TextCtrl(self, wx.ID_ANY, "")
self.btnSelectFeatures = wx.Button(self, wx.ID_ANY, "Select")
# selectEvent = lambda event, pathname=txt: self.dialogFeatures(event, pathname)
self.btnSelectFeatures.Bind(wx.EVT_BUTTON, self.dialogFeatures)
self.txtPipesPath = wx.TextCtrl(self, wx.ID_ANY, "")
self.btnSelectPipes = wx.Button(self, wx.ID_ANY, "Select")
self.bxOutput = wx.Panel(self, wx.ID_ANY)
self.cbxDraw = wx.CheckBox(self, wx.ID_ANY, "Draw")
self.btnClear = wx.Button(self, wx.ID_ANY, "Clear")
self.btnZoom = wx.Button(self, wx.ID_ANY, "Zoom")
self.btnRun = wx.Button(self, wx.ID_ANY, "Run", style=wx.BU_EXACTFIT)
self.__set_properties()
self.__do_layout()
# end wxGlade
def __set_properties(self):
# begin wxGlade: frmCheckSubmitall.__set_properties
self.SetTitle("Check Submittal")
self.rbxUtilitySelect.SetSelection(0)
self.btnSelectFeatures.SetMinSize((80, 20))
self.btnSelectPipes.SetMinSize((80, 20))
self.cbxDraw.SetValue(1)
self.btnClear.SetMinSize((50, 20))
self.btnZoom.SetMinSize((50, 20))
# end wxGlade
def __do_layout(self):
# begin wxGlade: frmCheckSubmitall.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_5 = wx.BoxSizer(wx.VERTICAL)
sizer_8 = wx.BoxSizer(wx.HORIZONTAL)
sizer_7 = wx.BoxSizer(wx.HORIZONTAL)
sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
sizer_5.Add(self.rbxUtilitySelect, 0, wx.ALIGN_CENTER | wx.BOTTOM, 10)
lblFeatures = wx.StaticText(self, wx.ID_ANY, "Features: ")
sizer_6.Add(lblFeatures, 0, wx.ALIGN_CENTER | wx.LEFT, 16)
sizer_6.Add(self.txtFeaturesPath, 1, 0, 0)
sizer_6.Add(self.btnSelectFeatures, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT, 5)
sizer_5.Add(sizer_6, 0, wx.EXPAND, 0)
lblPipes = wx.StaticText(self, wx.ID_ANY, "Pipes: ")
sizer_7.Add(lblPipes, 0, wx.ALIGN_CENTER | wx.LEFT | wx.RIGHT, 16)
sizer_7.Add(self.txtPipesPath, 1, 0, 0)
sizer_7.Add(self.btnSelectPipes, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT, 5)
sizer_5.Add(sizer_7, 0, wx.ALL | wx.EXPAND, 0)
sizer_5.Add(self.bxOutput, 1, wx.ALL | wx.EXPAND, 10)
sizer_8.Add(self.cbxDraw, 0, wx.LEFT | wx.RIGHT, 10)
sizer_8.Add(self.btnClear, 0, wx.RIGHT, 10)
sizer_8.Add(self.btnZoom, 0, 0, 0)
sizer_8.Add((20, 20), 1, 0, 0)
sizer_8.Add(self.btnRun, 0, wx.BOTTOM | wx.RIGHT, 10)
sizer_5.Add(sizer_8, 0, wx.EXPAND, 0)
sizer_1.Add(sizer_5, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
self.Layout()
self.SetSize((400, 300))
# end wxGlade
# Begin Dialog Method
def dialogFeatures(self, event):
# otherwise ask the user what new file to open
#with wx.FileDialog(self, "Select the Features File", wildcard="Text files (*.txt)|*.txt",
# style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
fileDialog = wx.FileDialog(self, "Select the Features File", wildcard="Text files (*.txt)|*.txt",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if fileDialog.ShowModal() == wx.ID_CANCEL:
return # the user changed their mind
# Proceed loading the file chosen by the user
pathname = fileDialog.GetPath()
self.txtFeaturesPath.SetValue = pathname
self.txtFeaturesPath.SetValue(pathname)
try:
with open(pathname, 'r') as file:
self.txtFeaturesPath = file
except IOError:
wx.LogError("Cannot open file '%s'." % newfile)
# End Dialog Method
# end of class frmCheckSubmitall
if __name__ == '__main__':
app=wx.PySimpleApp()
frame = frmCheckSubmital(parent=None, id=-1)
frame.Show()
app.MainLoop()
我试图做几件事,我只是被烧坏了,需要一些帮助。
我尝试过的一些事情: - 在对话框方法中添加第三个参数以返回该参数(只是不确定在哪里分配) - 使用lambda事件尝试使用构造函数赋值?
非常感谢任何帮助或见解。谢谢!
答案 0 :(得分:1)
正如其他人已经指出的那样,可以采用文本控件的SetValue
。但这是一个小的可运行的例子:
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
open_file_dlg_btn = wx.Button(self, label="Open FileDialog")
open_file_dlg_btn.Bind(wx.EVT_BUTTON, self.on_open_file)
self.file_path = wx.TextCtrl(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(open_file_dlg_btn, 0, wx.ALL, 5)
sizer.Add(self.file_path, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
def on_open_file(self, event):
wildcard = "Python source (*.py)|*.py|" \
"All files (*.*)|*.*"
dlg = wx.FileDialog(
self, message="Choose a file",
defaultDir='',
defaultFile="",
wildcard=wildcard,
style=wx.FD_OPEN | wx.FD_MULTIPLE | wx.FD_CHANGE_DIR
)
if dlg.ShowModal() == wx.ID_OK:
paths = dlg.GetPath()
print("You chose the following file(s):")
for path in paths:
print(path)
self.file_path.SetValue(str(paths))
dlg.Destroy()
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None,
title="File Dialogs Tutorial")
panel = MyPanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
答案 1 :(得分:0)
尝试:
self.txtFeaturesPath.SetValue(pathname)
你还有一些其他的错误"功能"在您的示例代码中,请注意。
答案 2 :(得分:0)
我意识到我所分配的不是fileDialog打开的值,而是fileDialog对象的ram中的位置。
解决方案如下
value = fileDialog.Directory + "\\" + fileDialog.filename
self.txtFeaturesPath.SetValue(value)
谢谢你!