At wits end trying to make .exe - python

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)

Related

Trying to call a global variable in another function and getting NameError:

First time poster, new to Python, so be easy on me. Also apologize for any formatting faux pas as again, I'm new here.
I'm writing a Python script to parse through a .csv and transfer the necessary information into a .xml format, and I'm trying to make it so that the end-user can select the .csv they want to use rather than having to actually open the code and copy/paste a filepath into the function.
I've got a basic GUI going with the tkinter module and I've got a function that opens a filedialog and allows the user to select a file.
From what I understand in what I've written, this function returns a filepath as a TextIO wrapper object that I'm then converting to a string, trimming down, and trying to store in a global variable for use in the function that will actually run the "conversion"; however, I am running into a NameError in the next function that says my variable is not defined.
Output is:
line 45, in <module>
with open(imported_csv, newline='') as csv:
NameError: name 'imported_csv' is not defined. Did you mean: 'import_csv'?
For now, I'm just trying to get the function to read the csv and output the data to the terminal so I can make sure that the function is actually reading the csv before I go any further.
I'm sure I'm just missing something really basic, but I may be going about this totally the wrong way. Please help! Code follows below:
`
import csv
from tkinter import *
from tkinter import filedialog
# from tkinter import ttk #<---for styles
### ~GUI code~ ###
## static GUI elements
root = Tk()
root.title("CSV to XML converter for Command camera servers")
root.geometry('1200x600')
subtitle = Label(root, text = "Select a csv to import:")
subtitle.grid(column=0, row=0)
## interactive GUI elements
def converting_text():
subtitle.configure(text = 'Converting, please wait...')
convert_button.configure(fg = 'grey')
convert_button = Button(root, text = "Convert Now", command = lambda: [converting_text(), conversion()])
convert_button.grid(column=1, row=7)
### import csv
selected_csv = Label(text = '')
selected_csv.grid(column=2, row=1)
def import_csv():
global imported_csv
imported_csv = filedialog.askopenfilename() #opens dialog window to select a file
imported_csv = open(imported_csv, 'r') #retrieves filepath from dialog
imported_csv = str(imported_csv) #converts filepath to a string
selected_csv.configure(text = imported_csv.lstrip("<_io.TextIOWrapper name=").rstrip(" mode='r' encoding='cp1252'>") + "'") #trims unnecessary chars from filepath
return imported_csv
choose_file_button = Button(root, text = "Browse files", command = import_csv)
choose_file_button.grid(column=1, row=2)
## ~conversion function code~ ##
with open(imported_csv, newline='') as csv:
def conversion():
csvread = csv.reader(csv, delimiter=',')
for row in csvread:
print(', '.join(row))
root.mainloop()
`

How do I open a file with tkinter and save the data to another variable?

I am learning to use tkinter and I cannot figure out how to open a text file and save the data so that I can use the data in other calculations. In my code below a button is created that when pressed asks for and opens a file. It then prints the content of the file in the console. If the file contains for example a single number, say 100, I can't figure out how to save that number as a variable like "a."
from tkinter.filedialog import askopenfile
root = Tk()
root.geometry('200x100')
# This function will be used to open
# file in read mode and only Python files
# will be opened
def open_file():
file = askopenfile(parent=root, filetypes =[('Text Files', '*.txt')])
if file is not None:
content = file.read()
print(content)
a = content
btn = Button(root, text ='Open', command = lambda:open_file())
btn.pack(side = TOP, pady = 10)
You are assigning the contents of the file to a variable declared within the function. It will be destroyed after the function finishes.
Declare the variable before the function
data = []
Append the content of the file to the container value within the function
def open_file(container):
file = askopenfile(parent=root, filetypes =[('Text Files', '*.txt')])
if file is not None:
content = file.read()
print(content)
# give the content to the data
data.append(content)
However if you are going to give the data to another widget using a instance of tk.StringVar() might be better as most widgets have a textvariable option.
data = StringVar()
And instead of appending StringVar uses the set() method.
data.set(content)
Resources for you:
https://www.pythontutorial.net/tkinter/tkinterstringvar/
And:https://www.delftstack.com/howto/python-tkinter/how-to-change-the-tkinter-button-text/

how to implement saving?

I need to make the button save the path to its application. That is, I bind, say, a shortcut to a button, everything works, but if I restart, everything needs to be done again. Please tell me how to implement saving?
my code:
import tkinter.filedialog as tfd
import tkinter as tk
import os
window = tk.Tk()
window.title("Мой Открыватель")
window.geometry("600x400")
window.resizable(False, False)
file_name = ""
def open():
global file_name
if file_name == "":
file_name = tfd.askopenfilename()
os.startfile(file_name)
else:
os.startfile(file_name)
if btn1["text"] == "":
btn1["text"] = file_name
btn1 = tk.Button(window, text="", command=open)
btn1.place(x = 20, y = 25)
window.mainloop()
You can save data in many different ways. Three such ways are:
Use a database, for example sqlite to store your data. Fetch your data when the program starts and populate the required fields. -> https://docs.python.org/3/library/sqlite3.html
Serialize your data with the use of pickle -> https://docs.python.org/3/library/pickle.html (Please note, pickle is not secure)
Use a .txt, .csv or other types of files. Write your data to the file, then when your program starts, load the data from the file.

Open my tkinter exe file, when open txt file

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

How to insert the content of my txt file in a widget with Python 2.7 on visual studio code?

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.

Categories