Hello Everyone,
In my application i need to select a file using open dialog
box. And then i dont need to open the file. I just need to display the
name of the selected file in a text control. And then open the file in
later part of the program. But i am not able to get the file name and
display it in the text control. Here is the sample code.


import wx
import os

Fname = '' #Global variable to hold the file name.
class ScrolledWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(350, 300))
panel = wx.Panel(self, -1)
txt1 = wx.TextCtrl(panel, -1, pos=(30, 100), size=(150, 20))
button11 = wx.Button(panel, -1, "Open", pos=(200,100))
self.Bind(wx.EVT_BUTTON, self.OnOpen, button11)
txt1.write(Fname)

self.Centre()
self.Show()

def OnOpen(self,event):
self.dirname = ''
dlg = wx.FileDialog(self, "Choose a file", self.dirname,"",
"*.*", wx.OPEN)
if dlg.ShowModal()==wx.ID_OK:
self.filename=dlg.GetFilename()
Fname = self.filename
self.dirname=dlg.GetDirectory()
dlg.Destroy()

app = wx.App()
ScrolledWindow(None, -1, 'Aliens')
app.MainLoop()


Any help is appreciated....

Rani

Dani AI

Generated

Good catch, — switching the TextCtrl to an instance attribute and binding the button let the handler access it. A couple of small improvements will make the result more reliable when you reopen the file later:

  • Keep and use the full path returned by the dialog (so you can reopen the file regardless of current working directory).
  • Use the TextCtrl API to set or append text (SetValue to replace, AppendText/WriteText to append) instead of an undefined write method.
  • Prefer an instance attribute (e.g. self.current_file) instead of a module-level Fname — assigning to a bare name inside a method creates a local variable unless you declare it global.

A compact pattern that avoids the scoping issue and shows only the filename in the control while storing the absolute path:

# inside your Frame __init__
self.path_ctrl = wx.TextCtrl(panel)
self.current_file = None

def on_open(self, event):
    dlg = wx.FileDialog(self,
                        "Open file",
                        wildcard="*.*",
                        style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
    if dlg.ShowModal() == wx.ID_OK:
        full_path = dlg.GetPath()
        self.current_file = full_path
        self.path_ctrl.SetValue(os.path.basename(full_path))  # show name only
    dlg.Destroy()

Notes and quick checks:

  • If you want the full path visible, call SetValue(full_path) instead of showing only the basename.
  • SetValue replaces the control contents; use AppendText/WriteText to append.
  • Depending on wxPython version, the open-style constant may be wx.FD_OPEN (Phoenix) or wx.OPEN (classic).

This is solved

import wx
import os

class ScrolledWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(350, 300))
panel = wx.Panel(self, -1)
self.txt1 = wx.TextCtrl(panel, -1, pos=(30, 100), size=(150, 20))
self.button11 = wx.Button(panel, -1, "Open", pos=(200,100))
self.button11.Bind(wx.EVT_BUTTON, self.OnOpen)
#txt1.write(name)

self.Centre()
self.Show()

def OnOpen(self,event):
self.dirname = ''
dlg = wx.FileDialog(self, "Choose a file", self.dirname,"", "*.*", wx.OPEN)
if dlg.ShowModal()==wx.ID_OK:
self.filename=dlg.GetFilename()
self.dirname=dlg.GetDirectory()

self.txt1.write(self.filename)
dlg.Destroy()


app = wx.App()
ScrolledWindow(None, -1, 'Aliens')
app.MainLoop()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.