I have a tkinter file, that opens txt files and insert that into a Text widget.
I used cx_Freeze to build it to an .exe file, but if I right click and open the txt file with this program it does nothing.
Do anyone know, how van I do that my program automatically read the file, even if it's not opened form the program.
Sorry for my bad english, if I was not clear, I mean the "open the program from the txt file" that if you right-click the .txt file, there is an option "open with..." and I choose my .exe program made in tkinter.
This is my code:
from tkinter import *
import re
import os
from tkinter import filedialog
root = Tk()
def open_nd():
rawfile = filedialog.askopenfilename(title = "Select file",filetypes = (("Nonexistent documents","*.nd"), ("Exsistant decoded files","*.ed")))
print(rawfile)
file = open(rawfile, "r")
a = file.read()
area = Text(root)
area.pack()
area.insert(END, a)
menu=Menu(root)
root.config(menu=menu)
filebar=Menu(menu)
menu.add_command(label="Open", command=open_nd)
root.mainloop()
Again, sorry fo my bad english, and thanks the answers!
If i understood you right do these steps:
Right Click -> Open With
Click here More apps
Look for another program on this pc
Choose your exe here
Related
Here is my code:
import qrcode
from tkinter import *
from tkinter import filedialog
root=Tk()
def choosefile():
global file
file=filedialog.askopenfile(mode='r',title='Choose a File')
choosefilebutton=Button(root,text='Choose a File',command=choosefile)
choosefilebutton.pack()
def submit():
qr=qrcode.make(file)
qr.save('qrcode.png')
choosefilebutton=Button(root,text='Submit',command=submit)
choosefilebutton.pack()
root.mainloop()
It makes a QR code but when I scan the QR code the result is:
<_io.TextIOWrapper name='/Users/charliezhang/Desktop/hello.png' mode='r' encoding='UTF-8'>
I'm new to Python and don't understand everything too well yet
Can someone please help?
You only get a file path and name data with askopenfile.
Should be something like:
f = open(file.name)
qr = qrcode.make(f.read())
f.close()
In Py 3.8.10, the following works:
def submit():
if file:
qr = qrcode.make(file.read())
qr.save('qrcode.png')
Here, file returned from filedialog.askopenfile() is not a string but a file-like object. So, adding the .read() pulls the data from the file. The filedialog.askopenfilename() method returns a string which is the name of the file.
I also add a check for file, since if the user hits the Cancel button from the Open File dialog, file gets set to None. So doing this check helps prevent another error when the user hits the Submit button afterwards.
I'm a new user of Qt and I'm trying to fix a persistent crashing issue that happens in my 'open file' dialogue. Whenever I open a file dialogue to select a file and decide to hit cancel instead, it causes python to stop working.
This is what the code looks like right now:
# get save file
savefile, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open file", "", "Files (*.mat)")
self.matlocation = savefile
I've tried using:
savefile, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open file", "", "Files (*.mat)")
if(!fileName.isEmpty()&& !fileName.isNull()){
self.matlocation = savefile
}
but this is still causing crashes. Am I missing something?
Thanks in advance.
Edit
It's part of a larger function:
So it's part of a function from a larger .py. The function as it stands starts by loading in a mat file:
def loadmat(self):
from PyQt5 import QtWidgets
import functions.plotting as plot
# get save file
savefile, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open file", "", "Files (*.mat)")
self.matlocation = savefile
# set mat location in GUI box
self.gui.samplelocBox.setPlainText(self.matlocation)
I have to create a program that allows me to read a .txt file that already exists and insert the content of that file in a text widget. Can someone help me ?
from Tkinter import *
fenetre = Tk()
champ_label = Label(fenetre, text="titres incorrectes")
champ_label.pack()
ligne_texte = Text(fenetre)
ligne_texte.pack()
fenetre.mainloop()
This is my text widget
Try this:
with open('yourfile', 'r') as myfile:
yourtext= myfile.read()
ligne_texte.insert(END, yourtext)
If the text is too long, you may have problems displaying the whole text. Maybe you should try a scrollbar in the textbox.
This code here should work:
from Tkinter import *
fenetre = Tk()
champ_label = Label(fenetre, text="titres incorrectes")
champ_label.pack()
ligne_texte = Text(fenetre)
ligne_texte.pack()
ligne_texte.insert(END, open('file.txt', 'r').read())
fenetre.mainloop()
Here is what it brings up:
Hope this helps.
Python 3.65, windows 7.
I am at my wits end. I've spent the last 2 weeks learning python from scratch. I just want to make small system utils and I finally made a little program and it simply will not run as a stand alone .exe file.
I have spent the last 2 days trying every suggestion I can find and nothing works. For me there is no point in investing all this time and hassle if I can't make executables.
What I am asking is can some kind person try to compile this on their windows machine and see if it's just me (or my computer Windows 7 64bit) that is the problem. I have read somewhere there are problems with win 7 with some exes.
If that's not possible could someone look at the code and see if there is anything obvious that might cause compile problems?
I have tried cx_freeze and pyinstaller. they make the exes. They run, show a dos box and then nothing.
I have even tried down-grading as far as python 2.7 and I am just in a right mess I don't know what else to do. I have really enjoyed learning Python and I have to get this sorted before continuing and it's driving me crazy, please help.
Steve.
import pyperclip
from tkinter import *
from ctypes import windll
import os
import time
#default folder for saves "c:\\cb-pastes"
#default txt file "c:\\cb-pastes\\saved.txt"
#================set up gui=======================
root = Tk()
#check if "c:\\cb-pastes" exists, if not, create folder
check_folder=os.path.isdir("c:\\cb-pastes")
if not check_folder:
os.makedirs("c:\\cb-pastes")
#button functions
def call_save():
ct=time.asctime() #get system time and date in ct
cb_txt = pyperclip.paste() #store clipboard in cb_txt
f = open("c:\\cb-pastes\\saved.txt","a") # open the file:
f.write ("\n") #newline
f.write (ct) #save date and time
f.write ("\n")
f.write (cb_txt) #append to text file
f.write ("\n")
f.write ("-----------------------------")
f.close() #Close the file
def call_clear(): #clear clipboard
if windll.user32.OpenClipboard(None):
windll.user32.EmptyClipboard()
windll.user32.CloseClipboard()
def call_view(): #open text file of saved clips
os.startfile('c:\\cb-pastes\\saved.txt')
def call_viewcb(): #open text file of current clipboard contents
cb_get = pyperclip.paste()
f = open("c:\\cb-pastes\\temp.txt","w")
f.write (cb_get)
f.close()
os.startfile('c:\\cb-pastes\\temp.txt')
#create window
root.title ("CBMan V0.7")
root.geometry ("230x132")
# create buttons
app = Frame(root)
app.grid()
button1 = Button(app, text = "Save Clipboard", command=call_save)
button1.grid(row=0,column=0)
button2 = Button(app, text = "Clear Clipboard", command=call_clear)
button2.grid()
button3 = Button(app, text = "View Saves ", command=call_view)
button3.grid()
button4 = Button(app, text = "View Clipboard ", command=call_viewcb)
button4.grid()
#insert logo
photo = PhotoImage(file="c:\\cb-pastes\\cbman.gif")
label = Label(image=photo)
label.image = photo # keep a reference!
label.grid(row=0,column=1)
I want to make a python script that opens a text file that the user can update before continuing (for configurations) and then continue the script once the text editor closes. Is there something in python that will allow me to do this?
on windows using notepad:
import os
cf = "config.txt" # or full path if you like
if not os.path.exists(cf):
f = open(cf,"w"); f.close() # create to avoid notepad message
os.system("notepad "+cf) # call waits until user closes the file
# now the file has been edited by the user