Python program can't handle running exes - python

My program stops working after successfully running the two exes. here is the code:
from tkinter import *
import os
root = Tk()
root.geometry('350x150')
root.title("hurler")
photo = PhotoImage(file = "Logo_Image.png")
root.iconphoto(False, photo)
entry_text = Label(text = "Number of posts you wish to automate (between 1-12) * ")
entry_text.place(x = 15, y = 10)
num = StringVar()
time_entry = Entry(textvariable = num, width = "10")
time_entry.place(x = 15, y = 40)
def action():
global num
num = num.get()
if num == '1':
os.startfile('.\\hurl\\hurl.exe')
# exit()
if num == '2':
os.startfile('.\\hurl\\hurl.exe')
os.startfile('.\\hurl2\\hurl.exe')
# exit()
num = StringVar()
register = Button(root,text = "Make", width = "10", height = "2", command = action, bg = "lightblue")
register.place(x = 15, y = 70)
root.mainloop()
hurl takes in text entries and then runs a web driver exe after pressing the Post button. Heres a sample block from code from hurl:
from tkinter import *
import os
from time import sleep
root = Tk()
root.geometry('600x600')
root.title("hurl")
photo = PhotoImage(file = "Logo_Image.png")
root.iconphoto(False, photo)
def save_post():
emailE = email_in.get()
file1 = open ("user.txt", "w")
file1.write(emailE)
file1.close()
passE = pass_in.get()
file2 = open ('pass.txt', 'w')
file2.write(passE)
file2.close()
entry1_text = Label(text = "Email * ",)
entry2_text = Label(text = "Password * ",)
entry1_text.place(x = 15, y = 70)
entry2_text.place(x = 15, y = 130)
email_in = StringVar()
pass_in = StringVar()
email_entry = Entry(textvariable = email_in, width = "30")
pass_entry = Entry(textvariable = pass_in, width = "30")
def app():
os.system(".\Auto_Post_With_Photo.exe")
def everything():
save_post()
app()
register = Button(root,text = "Post", width = "10", height = "2", command = everything, bg = "lightblue")
register.place(x = 15, y = 380)
root.mainloop()
I'm hoping I don't need to show the last program to get an answer to the issue. The Issue I'm having is that the program runs perfectly until the button is pressed, and then the hurls exes crash.
But if I click on the two hurl.exe with my mouse and not this 3rd program pressing the post button won't crash.
Any ideas to fix this issue? Also, it's not due to the 'exit()s', it does the same thing with or without them. :)

Related

First tkinter project ,trying to find a way to make my tkinter buttons change boolean values for future if statements

I couldnt seem to find a way to make my program realise that ive selected a button, so i changed the function of the celcius to farenheit to try to make it change a boolean value to determine what conversion the program is doing
def celcius_to_farenheit(_event = None):
c_to_f = True
f_to_c = False
the idea being later i can use if statments later in the end result function to find what conversion its doing and display results in the status bar
def end_result():
if c_to_f == True:
converted_temperature = (valid_temperature * 9/5) + 32
label_status.configure(text = converted_temperature, fg = "Orange")
currently i seem to have functions running without me pressing buttons as well, when start the program it immediatly goes to the error message ive created for input muct be numeric even if i havent pressed the celcius to farenheit button.
Any help regarding how to propely have my celcius to farenheit and farenheit to celcius buttons confirm its a float and change a value to use for determining which calculation its using would be helpfull. Knowing why the error message comes up automatically is a bonus.
Below is my code thank you for your time and help.
import sys
from tkinter import *
from tkinter.tix import *
c_to_f = True
def clear_reset(_event = None):
entry_temperature.delete(0, END)
label_status.configure(text = "All data cleared", fg = "Orange")
def end_program(_event = None):
sys.exit()
def convert_temp(_event = None):
try:
valid_temperature = float(entry_temperature.get())
except:
label_status.configure(text = "Input must be numeric", fg = "Orange")
def end_result():
if c_to_f == True:
converted_temperature = (valid_temperature * 9/5) + 32
label_status.configure(text = converted_temperature, fg = "Orange")
def celcius_to_farenheit(_event = None):
c_to_f = True
f_to_c = False
def farenheit_to_celcius(_event = None):
f_to_c = True
c_to_f = False
window = Tk()
window.geometry("550x200")
window.resizable(False, False)
window.title("Temperature Conversion")
tooltip = Balloon(window)
label_input_Temperature = Label(text = "Temperature",fg = "Green")
label_input_Temperature.grid(row= 0, column=0)
entry_temperature = Entry(window, bg = "light blue" )
entry_temperature.grid(row=0, column=1)
temp_button_c_to_f = Button(window, text = "Celcius to Farenheit", command = celcius_to_farenheit)
temp_button_c_to_f.grid(row = 1, column=0)
window.bind('<Shift-c>', celcius_to_farenheit)
tooltip.bind_widget(temp_button_c_to_f, msg = "Shift + C")
temp_button_f_to_c = Button(window, text = "Farenheit to Celcius")
temp_button_f_to_c.grid(row = 1, column = 1 )
conversion_button = Button(window, text = "Convert", command = convert_temp)
conversion_button.grid(row = 2, column = 0,padx =0 )
window.bind('<Enter>', convert_temp)
tooltip.bind_widget(conversion_button, msg = "Enter")
clear_button = Button(window, text = "Clear", command = clear_reset)
clear_button.grid(row = 2, column = 1)
window.bind('<Control-c>', clear_reset)
tooltip.bind_widget(clear_button, msg = "Ctrl + C")
exit_button = Button(window, text = "Exit")
exit_button.grid(row = 2, column = 2, padx = 20, pady = 20)
window.bind('<Control-x>', end_program)
tooltip.bind_widget(exit_button, msg = "Ctrl + X")
label_status = Label(window, width = 50, borderwidth = 2, relief= RIDGE,bg= "Grey" )
label_status.grid(row = 4, column = 1)
tooltip.bind_widget(label_status, msg = "Displays results / error messages")
label_status.configure(text = "Enter in your temperature and select your conversion", fg = "Orange")
window.mainloop()

How to decrease the size of an entry in tkinter python

I want to decrease the size of an entry in tkinter python. ive already asked about label but i havent used fyi
the code:
from tkinter import *
from tkinter import messagebox
top = Tk()
top.title("Hello wold")
top.geometry("300x300")
ei = StringVar()
ei1 = StringVar()
def karma():
try:
ee = ei.get()
ee1 = ei1.get()
ez = int(ee) + int(ee1)
return messagebox.showinfo('message', f'{ee} + {ee1} = {ez}')
except ValueError:
return messagebox.showinfo('message', f'ERROR')
name = Label(top,text="1st no. : ").place(x = 20,y = 50)
e = Entry(top,textvariable=ei).place(x = 100, y = 50)
name1 = Label(top,text="2nd no. : ").place(x = 20,y = 100)
e1 = Entry(top,textvariable=ei1).place(x = 100, y = 100)
b = Button(top, text="Click here", command=karma).place(x = 20,y = 150)
top.mainloop()
you may use the font option in Entry to adjust the size.
Here is an example (to change it to 8pts)
import tkFont
helv8 = tkFont.Font(family='Helvetica', size=8)
e = Entry(top, font = helv8, textvariable=ei).place(x = 100, y = 50)

i am trying to store some data into a text file, there are no errors but it writes ".!toplevel.!entrywrite" instead of user input

i am making a sign up page and im trying to store the email the user entered to a text file but it doesnt seem to work it stores ".!toplevel.!entrywrite" instead of user input. i am new to this python and tkinter so i dont really know what to do, the code is a little bit long sorry about that.
Any help will be appreciated. Thank you
from tkinter import*
from PIL import Image, ImageTk
import tkinter as tk
root = Tk()
root.geometry('670x466')
class Goode_brothers:
def __init__(self, parent):
myFrame = Frame(parent)
myFrame.pack()
self.load = Image.open('new-dip-project\\food.jpg')
self.render = ImageTk.PhotoImage(self.load)
self.img = Label(parent, image = self.render)
self.img.place(x = -26, y =0)
self.img_login = PhotoImage(file = 'new-dip-project\\button (3).png')
self.b1 = Button(parent,image = self.img_login,bd = 0, bg = '#3b353b', activebackground = '#3b353b')
self.b1.place(x = 275, y = 310)
self.img_register = PhotoImage(file = 'new-dip-project\\register.png')
self.b2 = Button(parent,image = self.img_register, command = self.openNewWindow, bd = 0, bg = '#3b353b', activebackground = '#3b353b')
self.b2.place(x = 265, y = 400)
self.canvas = Canvas(parent, width = 400, height = 120)
self.canvas.pack()
self.img4 = ImageTk.PhotoImage(Image.open('new-dip-project\\goode.png'))
self.canvas.create_image(20, 20, anchor=NW, image=self.img4)
self.email = Entry(parent).place(x = 340, y = 180)
self.password = Entry(parent).place(x = 340, y = 250)
self.img_label = PhotoImage(file = 'new-dip-project\\label-image.png')
self.name = Label(parent, image = self.img_label, text = "Email:", bg = '#3c3a3b').place(x = 197,y = 178)
self.img_label_pass = PhotoImage(file = 'new-dip-project\\label_pass.png')
self.name = Label(parent, image = self.img_label_pass, text = "Password:", bg = '#3c3a3b').place(x = 177,y = 245)
def create_pass(self):
self.password_length = Label(self.root2, text = '')
self.password_length.place(x = 80, y = 140)
self.pass_word = str(self.password2.get()) #this code is getting the users input from the password entry box
if len(self.pass_word) >= 8: #if the characters gotten from the pasword entry is less than 8, an erorr message will appear
self.registered = Label(self.root2, text = 'You have successfully registered, this window will now automatically close', font=("open sans", "8"))
self.registered.place(x = 80, y = 140)
self.root2.after(4000, self.root2.destroy)
else:
self.password_length.configure(text="""Your password must be atleast eight characters long. Please try again""", font=("open sans", "8"))
def save_info(self):
self.email_reg = str(self.email2.get())
print(self.email2)
file = open('emails.txt', 'w')
file.write(str(self.email2))
def create_email(self):
self.username_length = Label(self.root2, text = '', font = '40')
self.username_length.place(x = 165, y = 140)
self.email_reg = str(self.email2.get())
if len(self.email_reg) >= 1: #if user has inputted a letter or number it will allow it to go to the next function
self.save_info()
self.create_pass()
self.username_length.destroy()
else:
self.username_length.configure(text='Please enter your username or password', font=("open sans", "8"))
self.username_length.after(3000, self.username_length.destroy)
def openNewWindow(self):
# Toplevel object which will
# be treated as a new window
self.root2 = Toplevel(root)
# sets the title of the
# Toplevel widget
self.root2.title("New Window")
# sets the geometry of toplevel
self.root2.geometry("500x300")
self.load2 = Image.open('new-dip-project\\registerscreen3.jpg')
self.render2 = ImageTk.PhotoImage(self.load2)
self.img2 = Label(self.root2, image = self.render2)
self.img2.place(x = -2, y =0)
self.img_label2 = PhotoImage(file = 'new-dip-project\\label-image.png')
self.name = Label(self.root2, image = self.img_label, bg = '#292929').place(x = 130,y = 102)
self.img_label_pass2 = PhotoImage(file = 'new-dip-project\\label_pass.png')
self.name = Label(self.root2, image = self.img_label_pass, bg = '#292929').place(x = 120,y = 173)
self.email2 = Entry(self.root2)
self.email2.place(x = 280, y = 104)
self.password2 = Entry(self.root2)
self.password2.place(x = 280, y = 180)
self.img_register2 = PhotoImage(file = 'new-dip-project\\register.png')
self.b3 = Button(self.root2,image = self.img_register2, command = self.create_email, bd = 0, bg = '#0d0d0d', activebackground = '#0d0d0d')
self.b3.place(x = 180, y = 250)
self.img_reg2 = PhotoImage(file = 'new-dip-project\\regtitle.png')
self.name9 = Label(self.root2, image = self.img_reg2, bg = '#131313')
self.name9.place(x = 109, y = 10)
if __name__ == "__main__":
e = Goode_brothers(root)
root.title('Goode brothers')
root.mainloop()
Using var = StringVar() to set option textvariable - var when initialize Entry, and get content of Entry by var.get().
example Code
from tkinter import *
def on_click():
print(entry_text.get())
root = Tk()
font = ("Courier New", 32)
entry_text = StringVar()
entry = Entry(root, textvariable=entry_text, font=font)
entry.place(x=0, y=0)
button = Button(root, text='Check', command=on_click, font= font)
button.place(x=0, y=64)
root.mainloop()
Following statement will get value None for self.email
self.email = Entry(parent).place(x = 340, y = 180)
should be
self.email = Entry(parent)
self.email.place(x = 340, y = 180)

Using a GUI to write to a CSV file cells

I am trying to make a GUI that will write to a CSV file. I want whatever I put into the entry boxes to write to a corresponding cell.
An example of entries I want to see in the CSV is:
vlan 8
192.168.1.1 255.255.255.0
fa2/0/1
however, when I open the data1.csv it writes:
PY_VAR0
PY_VAR1
PY_VAR2
Here is the python code I wrote:
import csv
from tkinter import *
root = Tk()
root.geometry('300x300')
root.title("Cisco-Oneport")
photo = PhotoImage(file = "Logo.PNG")
root.iconphoto(False, photo)
def info():
list_of_lists = [[f'{vlan}',],
[f'{ip}',],
[f'{port}',]]
with open('data1.csv', 'w', newline='') as f:
writer = csv.writer(f, delimiter=';')
for sublist in list_of_lists:
writer.writerow(sublist)
entry1_text = Label(text = "Type Vlan Number * ",)
entry2_text = Label(text = "Type IP Number and Subnet * ",)
entry3_text = Label(text = "Type in Fast Port * ",)
entry1_text.place(x = 15, y = 30)
entry2_text.place(x = 15, y = 90)
entry3_text.place(x = 15, y = 150)
vlan = StringVar()
ip = StringVar()
port = StringVar()
vlan_entry = Entry(textvariable = vlan, width = "10")
ip_entry = Entry(textvariable = ip, width = "30")
port_entry = Entry(textvariable = port, width = "10")
vlan_entry.place(x = 15, y = 60)
ip_entry.place(x = 15, y = 120)
port_entry.place(x = 15, y = 180)
register = Button(root,text = "Run", width = "10", height = "2", command = info, bg = "lightgreen")
register.place(x = 15, y = 240)
root.mainloop()
How do I get the desired inputs? I'm pretty new to light GUI coding.
The .csv file is showing the tcl/tk names for the StringVars. Use .get() to get the values. The Labels and Entries were created without a containing object so I was seeing one GUI with the Text and a separate one with the run button.
It's easier to answer questions where the code can be copied and run in a console. Therefore it's better to remove things like icons which use files that won't exist on another PC.
import csv
from tkinter import *
root = Tk()
root.geometry('300x300')
root.title("Cisco-Oneport")
# Removed icon as I cant access the file
# Use .get() to get the values of the StringVars, not the names of the StringVars
def info():
list_of_lists = [[f'{vlan.get()}',],
[f'{ip.get()}',],
[f'{port.get()}',]]
with open('data1.csv', 'w', newline='') as f:
writer = csv.writer(f, delimiter=';')
for sublist in list_of_lists:
writer.writerow(sublist)
# Add root to put the all the objects in the same GUI
entry1_text = Label(root, text = "Type Vlan Number * ",)
entry2_text = Label(root, text = "Type IP Number and Subnet * ",)
entry3_text = Label(root, text = "Type in Fast Port * ",)
entry1_text.place(x = 15, y = 30)
entry2_text.place(x = 15, y = 90)
entry3_text.place(x = 15, y = 150)
vlan = StringVar()
ip = StringVar()
port = StringVar()
# Add root to put the all the objects in the same GUI
vlan_entry = Entry(root, textvariable = vlan, width = "10")
ip_entry = Entry(root, textvariable = ip, width = "30")
port_entry = Entry(root, textvariable = port, width = "10")
vlan_entry.place(x = 15, y = 60)
ip_entry.place(x = 15, y = 120)
port_entry.place(x = 15, y = 180)
#
register = Button(root,text = "Run", width = "10", height = "2", command = info, bg = "lightgreen")
register.place(x = 15, y = 240)
root.mainloop()

Changing the show of an entry box numerous times

I would like the contents of this my entry box to be uncovered from the * when the button is clicked, but then recovered when it is clicked again, and so on. Any ideas?
The code i have currently:
password_entry = Entry(root, width = 45, textvariable=user_password, show = "*")
def Show():
password_entry.config(show="")
show_password = Button(canvas, width = 31, height = 17, image = eye_img, bg = "gray33", relief = "flat", command = Show)
This only uncovers the password, and does not recover it when the button is reclicked.
Its seems that you are trying to make a toggle button. Edit your function. Get the value of current show. If it is * then change it to , if it is then change it to *. Your function can be:
def Show():
password_entry["show"] = "*" if password_entry["show"] == "" else ""
Here is a sample code that works like expected:
from tkinter import *
root = Tk()
password_entry = Entry(root, width = 45, show = "*")
password_entry.grid(row=0,column=0)
def Show():
password_entry["show"] = "*" if password_entry["show"] == "" else ""
show_password = Button(root,text="Show Password",bg = "gray33", relief = "flat", command = Show)
show_password.grid(row=0,column=1)
root.mainloop()

Categories