How to lock contents in wxTextCtrl? - python

I am trying to implement a simple application. It use wx.FileDialog to select a file, then display the file name in a wx.TextCtrl component.
It works allright at first, the file name was displayed as expected. However, when the mouse move over the text control component, the contents are gone.
Here is my code:
fdlg_input_dir = wx.FileDialog(dlg_input, "Choose input file", os.getcwd(), "", "All files(*.*)|*.*", wx.OPEN)
fdlg_input_dir.ShowModal()
textctrl_input_dir = wx.TextCtrl(dlg_input, 5, fdlg_input_dir.GetPath(), size=(300,20), pos=(85,20))
So my questions is, how to remain the contents in the TextCtrl component while the mouse moves over it?
Any suggestions are appreciated. Thanks.

Problem solved. It turns out that the initial text in the control component doesn't stand. Instead, using following code should archieve what I expected:
textctrl_input_dir.write(fdlg_input_dir.GetPath())

Related

Running find_all() commands in a new view via Sublime Text Plugin changes it into an Instant File Search

I am building an internal plugin for Sublime Text 3 to automate some changes we need done in our HTML.
I'm close to finishing it so we can start using it but I just have one small issue I can't seem to find a solution for.
The plugin will search for some specific HTML code, save them in a file and then modify the code in the view to reference some text in a json file. Then the plugin will open a new view and paste the contents of the file we saved some code in step 1. After which, I need to do some formatting in the new view (replace commas, quotes and brackets with a new line) and then run another internal sublime command in the new view.
The issue is after replacing the commas, quotes and brackets with a new line, the new view changes to an "Instant File Search" which messes with how the view runs the next command and inserts some garbage characters.
Example of the code:
#Open new tab and paste the copied subheads
newView = self.view.window().new_file(syntax="HTML.sublime-syntax")
newView.insert(edit, 0, json.dumps(sections))
# Clear any current selections in the new view
newView.sel().clear()
# Remove quotes, commas and square brackets and separate into new lines
q = newView.find_all(r"(\x{0022}\,\s\x{0022})|(\[\x{0022})|(\x{0022}\])")
for region in reversed(q):
newView.sel().add(region)
newView.replace(edit, region, "\n")
# Clear any current selections in the new view
newView.sel().clear()
q = newView.find_all("{% r")
for region in reversed(q):
newView.sel().add(region)
newView.replace(edit, region, "<p>{% r")
# Clear any current selections in the new view
newView.sel().clear()
q = newView.find_all("endreplace %}")
for region in reversed(q):
newView.sel().add(region)
newView.replace(edit, region, "endreplace %}</p>")
This is what it outputs in the new view:
If I click undo, I get the expected output which seems to mean sublime does something extra after running the last find_all code.
I haven't found anything online regarding this issue so I'm unable to try things to fix this.
Does anyone have an idea on how I should proceed?
P.S. One possible solution is to manually open a new view and run some commands but I'm trying to do all of that in 1 step using my plugin.

How to save DataFrame in the same location where the program will be

Hi I write program that will take many csv filles make three new structure for them.
After that program concat the same csv files in one structure depends of its name.
All this i done alredy but i need to save this three new files. I did it by using tkinter and it works, code look like this:
list_of_df = [completeDR,completeDS,completeDD,completeKL]
for i in list_of_df:
if not i.empty:
root = Tk() # this is to close the dialogue box later
try:
# with block automatically closes file
with filedialog.asksaveasfile(mode='w', defaultextension=".csv") as file:
i.to_csv(file.name, index = False)
except AttributeError:
# if user cancels save, filedialog returns None rather than a file object, and the 'with' will raise an error
print("The user cancelled save")
root.destroy() # close the dialogue box
This solutions work rly good but I have one problem. When I save files I need to named it but i dont know which one i save in this moment. I need solution that let me save files exacly in the same place where application will be run. Second option is to change name of title in tkinter winodw. If title will say me what i save right now there will be no problem anymore. Anyone have some ideas ? Thanks for help.

How do you get filenames and save and display there names in labels. KivyMD Kivy Python

I am trying to display file names on Kivy Buttons but I want to add a feature so when there is not txt file the Buttons don't show up but the more txt files that you have the more labels you have.
I have a system where you save text to a file a give the file a title so I want the user to know if this file exists. I currently have no knowledge on how to do this. My idea is when you press the button with the file name for example "test" it will display the text of the txt file but I don't know how to display the names of the txt files on the buttons and there might not be any txt files so you would have to hide the buttons. I am using KivyMD so those buttons would be preferred.
Here is a screenshot of the page that the buttons would be displayed on:
I hope the experts of python could help me out here.
Update:
files_list = [os.path.abspath(x) for x in os.listdir()]
for k in files_list:
type = mimetypes.MimeTypes().guess_type(k)[0]
if type == "text\plain":
MainScreen.add_widget(Button(text=k))
Use two screens approach. First screen for list of buttons and second for editing text.
import mimetypes, os
files_list = [os.path.abspath(x) for x in os.listdir()]
for k in files_list:
type = mimetypes.MimeTypes().guess_type(k)[0])
if type == "text\plain":
mylayout.add_widget(Button(text=k))
Here mylayout is a kivy layout depends upon how you want to arrange your buttons.
For displaying text, second screen would contain a label. Just read the text file and assign it to text property of label.

How to allow tkinter Label or Message widget to split words when wrapping

I would look to display a path to a file in the tkinter application i'm developing and would like the Label or Message widget not to wrap the text when it sees a space character but instead when the text exceeds the set wraplength.
Currently when i'm displaying a really long path it does it like this:
Current file:
/home/exampleName/A-Path/A-Really-long-Path/Running-out-ideas/ok-another-directory/my_file.txt
but instead i would prefer if it would display like this:
Current file: /home/exampleName/A-Path/A-Really-long-Path/Running-out-ideas/ok-another-directory/my_file.txt
any ideas or suggestions? Thanks for your help i would appreciate it a lot.

Save Contents of QTextEdit as *.pdf?

I am trying to save the contents of a Text Editor as a pdf file. The text Editor has been made using PyQt (i didn't make the text Editor), i got the code of the text editor from here. I have done some changes to the editor but that wont be a problem.
After some initial research i found that i need to use ReportLab to publish a pdf file.But i can't find a way to do this .
Does anyone know how this could be accomplished ?
The source code for the Text Editor already has a PDF method, but it is unused, and possibly won't work properly as it stands.
A basic re-write of the method that should work on all platforms, would look like this:
def SavetoPDF(self):
filename = QtGui.QFileDialog.getSaveFileName(self, 'Save to PDF')
if filename:
printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)
printer.setPageSize(QtGui.QPrinter.A4)
printer.setColorMode(QtGui.QPrinter.Color)
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
printer.setOutputFileName(filename)
self.text.document().print_(printer)
The only other thing you'll need is a menu item to run it, so in Main.initUI just add:
pdfAction = QtGui.QAction("Save to PDF", self)
pdfAction.setStatusTip("Save to PDF")
pdfAction.triggered.connect(self.SavetoPDF)
...
file.addAction(pdfAction)

Categories

Resources