I've made a really simple desktop app using Tkinter. When I set up everything in py2app the program looks like its generated fine, but when I click on it to open my computer restarts everytime. The code will run fine when I run it out of the Python IDE, but my screen goes black and restarts and brings me back to the MacOS login page when I try to run it by clicking on the icon or running the py2app version from the terminal. Has anyone run into this issue?
from tkinter import *
import tkinter
# importing everyting from tkinter
# expression to access among all the functions
expression = ""
# functions
def input_number(number, equation):
# accessing the global expression variable
global expression
# concatenation of string
expression = expression + str(number)
equation.set(expression)
def clear_input_field(equation):
global expression
expression = ""
# setting empty string in the input field
equation.set("Enter the expression")
def evaluate(equation):
global expression
# trying to evaluate the expression
try:
result = str(eval(expression))
# showing the result in the input field
equation.set(result)
# setting expression to empty string
expression = ""
except:
# some error occured
# showing it to the user equation.set("Enter a valid expression")
expression = ""
def new_calc():
new_window = Tk()
new_window.title("A whole new window")
# set the configuration of GUI window
new_window.geometry("450x220")
print('This is a new window!')
# creating the GUI
def main():
# main window
window = Tk()
# setting the title of GUI window
window.title("Calculator")
# set the configuration of GUI window
window.geometry("450x220")
# varible class instantiation
equation = StringVar()
# input field for the expression
input_field = Entry(window, textvariable=equation)
input_field.place(height=100)
# we are using grid position
# for the arrangement of the widgets
input_field.grid(columnspan=4, ipadx=100, ipady=5)
# settin the placeholder message for users
equation.set("Enter the expression")
# creating buttons and placing them at respective positions
_1 = Button(window, text='1', bd=0, command=lambda: input_number(1, equation), height=2, width=7)
_1.grid(row=2, column=0)
_2 = Button(window, text='2', bd=0, command=lambda: input_number(2, equation), height=2, width=7)
_2.grid(row=2, column=1)
_3 = Button(window, text='3', bd=0, command=lambda: input_number(3, equation), height=2, width=7)
_3.grid(row=2, column=2)
_4 = Button(window, text='4', bd=0, command=lambda: input_number(4, equation), height=2, width=7)
_4.grid(row=3, column=0)
_5 = Button(window, text='5', bd=0, command=lambda: input_number(5, equation), height=2, width=7)
_5.grid(row=3, column=1)
_6 = Button(window, text='6', bd=0, command=lambda: input_number(6, equation), height=2, width=7)
_6.grid(row=3, column=2)
_7 = Button(window, text='7', bd=0, command=lambda: input_number(7, equation), height=2, width=7)
_7.grid(row=4, column=0)
_8 = Button(window, text='8', bd=0, command=lambda: input_number(8, equation), height=2, width=7)
_8.grid(row=4, column=1)
_9 = Button(window, text='9', bd=0, command=lambda: input_number(9, equation), height=2, width=7)
_9.grid(row=4, column=2)
_0 = Button(window, text='0', bd=0, command=lambda: input_number(0, equation), height=2, width=7)
_0.grid(row=5, column=0)
plus = Button(window, text='+', bd=0, command=lambda: input_number('+', equation), height=2, width=7)
plus.grid(row=2, column=3)
minus = Button(window, text='-',bd=0, command=lambda: input_number('-', equation), height=2, width=7)
minus.grid(row=3, column=3)
multiply = Button(window, text='*', bd=0, command=lambda: input_number('*', equation), height=2, width=7)
multiply.grid(row=4, column=3)
divide = Button(window, text='/', bd=0, command=lambda: input_number('/', equation), height=2, width=7)
divide.grid(row=5, column=3)
equal = Button(window, text='=', bd=0, command=lambda: evaluate(equation), height=2, width=7)
equal.grid(row=5, column=2)
clear = Button(window, text='Clear', bd=0, command=lambda: clear_input_field(equation), height=2, width=7)
clear.grid(row=5, column=1)
# showing the GUI
window.mainloop()
# start of the program
if __name__ == '__main__':
main()
Here is the setup file:
from setuptools import Extension, setup
APP = ['CalcApp.py']
DATA_FILES = []
OPTIONS = {
'iconfile':'Calculator.icns',
'argv_emulation': True,
'packages': ['certifi'],
}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Related
I have a tkinter GUI code that executes different functions which are mapped to different buttons/widgets, I want to give users the option of choosing to open the GUI window or directly execute from the command line.
I tried keeping mainloop() inside an optional switch like:
if gui == "1":
root.mainloop()
else:
#command mode
# call required functions here using user switches.
Is this the correct way of doing this? this is working when i tried in linux system.
root = Tk()
root.title('ATM v.keypad buttons //////dont acc work///////')
# creates the buttons adds no value tho
# size of button
gridlabel1 = Button(root, text='1', padx=50, pady=50)
gridlabel2 = Button(root, text='2', padx=50, pady=50)
gridlabel3 = Button(root, text='3', padx=50, pady=50)
gridlabel4 = Button(root, text='4', padx=50, pady=50)
gridlabel5 = Button(root, text='5', padx=50, pady=50)
gridlabel6 = Button(root, text='6', padx=50, pady=50)
gridlabel7 = Button(root, text='7', padx=50, pady=50)
gridlabel8 = Button(root, text='8', padx=50, pady=50)
gridlabel9 = Button(root, text='9', padx=50, pady=50)
gridlabel0 = Button(root, text='0', padx=50, pady=50)
cancelbutton = Button(root, text='cancel', padx=75, pady=50)
clearbutton = Button(root, text='clear', padx=75, pady=50)
enterbutton = Button(root, text='enter', padx=75, pady=50)
# prints the button at set location
gridlabel1.grid(row=0, column=0)
gridlabel2.grid(row=0, column=1)
gridlabel3.grid(row=0, column=2)
gridlabel4.grid(row=1, column=0)
gridlabel5.grid(row=1, column=1)
gridlabel6.grid(row=1, column=2)
gridlabel7.grid(row=2, column=0)
gridlabel8.grid(row=2, column=1)
gridlabel9.grid(row=2, column=2)
gridlabel0.grid(row=3, column=1)
cancelbutton.grid(row=0, column=4)
clearbutton.grid(row=1, column=4)
enterbutton.grid(row=2, column=4)
root.mainloop()
that was my example notice how I put root.mainloop() at the end
was indented because it was in a if statement
I'm getting an indent error in my Eclipse compiler, I have marked the specific line below on for where the error is. Anyone have any idea on the fix? It's for an assignment at school and I have no idea on a fix and I'm at a complete, absolute loss. I would appreciate anyone's tips. "Write a GUI based program that simulates a simple pocket calculator. The GUI displays a single entry field for output (I would suggest to make it a read only field). It should present the user with 10 numeric buttons from 0 to 9 and seven function keys for +, -, *, /, C (C is for clearing the display), . (decimal point) and =. = is to calculate the correct answer. If there is an error like divide by zero the entry field should display ERR or Error. Values in the entry field will build a string that will be converted to a float for calculation." This is the assignment I have to complete.
from tkinter import *
from tkinter import font as tkFont # for convenience
expression = ""
# functions
def input_number(number, equation):
global expression
# concatenation of string
expression = expression + str(number)
equation.set(expression)
def clear_input_field(equation):
global expression
expression = ""
# setting empty string in the input field
equation.set("Enter the expression")
def evaluate(equation):
global expression
# trying to evaluate the expression
try:
result = str(eval(expression))
equation.set(result)
expression = ""
except:
equation.set("Error: Divide by zero")
expression = ""
def main():
# creating main window
tk = Tk()
# setting the title
tk.title("Calculator")
# seting the size of window
tk.geometry("570x500")
# varible class instantiation
equation = StringVar()
# input field for the expression
input_field = Entry(tk, textvariable=equation)
input_field.place(height=100)
input_field.grid(columnspan=4, ipadx=200, ipady=10)
equation.set("Enter the expression")
myFont = tkFont.Font(family='Helvetica', size=12, weight='bold')
input_field['font']=myFont
# creating buttons and placing them at respective positions
_1 = Button(tk, text='1', fg='black', bg='red', bd=2, command=lambda: input_number(1, equation), height=5, width=10) #I'm getting an error on this line
_1['font'] = myFont
_1.grid(row=2, column=0)
_2 = Button(tk, text='2', fg='black', bg='red', bd=2, command=lambda: input_number(2, equation), height=5, width=10)
_2.grid(row=2, column=1)
_3 = Button(tk, text='3', fg='black', bg='red', bd=2, command=lambda: input_number(3, equation), height=5, width=10)
_3.grid(row=2, column=2)
_4 = Button(tk, text='4', fg='black', bg='red', bd=2, command=lambda: input_number(4, equation), height=5, width=10)
_4.grid(row=3, column=0)
_5 = Button(tk, text='5', fg='black', bg='red', bd=2, command=lambda: input_number(5, equation), height=5, width=10)
_5.grid(row=3, column=1)
_6 = Button(tk, text='6', fg='black', bg='red', bd=2, command=lambda: input_number(6, equation), height=5, width=10)
_6.grid(row=3, column=2)
_7 = Button(tk, text='7', fg='black', bg='red', bd=2, command=lambda: input_number(7, equation), height=5, width=10)
_7.grid(row=4, column=0)
_8 = Button(tk, text='8', fg='black', bg='red', bd=2, command=lambda: input_number(8, equation), height=5, width=10)
_8.grid(row=4, column=1)
_9 = Button(tk, text='9', fg='black', bg='red', bd=2, command=lambda: input_number(9, equation), height=5, width=10)
_9.grid(row=4, column=2)
_0 = Button(tk, text='0', fg='black', bg='red', bd=2, command=lambda: input_number(0, equation), height=5, width=10)
_0.grid(row=5, column=0)
plus = Button(tk, text='+', fg='black', bg='red', bd=2, command=lambda: input_number('+', equation), height=5, width=10)
plus.grid(row=2, column=3)
minus = Button(tk, text='-', fg='black', bg='red', bd=2, command=lambda: input_number('-', equation), height=5, width=10)
minus.grid(row=3, column=3)
multiply = Button(tk, text='*', fg='black', bg='red', bd=2, command=lambda: input_number('*', equation), height=5, width=10)
multiply.grid(row=4, column=3)
divide = Button(tk, text='/', fg='black', bg='red', bd=2, command=lambda: input_number('/', equation), height=5, width=10)
divide.grid(row=5, column=3)
equal = Button(tk, text='=', fg='black', bg='red', bd=2, command=lambda: evaluate(equation), height=5, width=10)
equal.grid(row=5, column=2)
_1['font'] = myFont
_2['font'] = myFont
_3['font'] = myFont
_4['font'] = myFont
_5['font'] = myFont
_6['font'] = myFont
_7['font'] = myFont
_8['font'] = myFont
_9['font'] = myFont
_0['font'] = myFont
plus['font'] = myFont
minus['font'] = myFont
multiply['font'] = myFont
divide['font'] = myFont
equal['font'] = myFont
clear = Button(tk, text='Clear', fg='black', bg='red', bd=2, command=lambda: clear_input_field(equation), height=5, width=10)
clear.grid(row=5, column=1)
clear['font']=myFont
# showing the GUI continue
tk.mainloop()
if __name__ == '__main__':
main()```
The line you've highlighted, and the following lines, are indented by an extra step - make sure they line up with the line above, as they aren't inside another block (e.g a function or an if statement)
I'm new to tkinter and i've built this so far:
import tkinter as tk
import subprocess
import os
def maingui():
window = tk.Tk()
window.title("My first GUI")
#window.state('zoomed')
window.geometry("700x205")
window.configure(bg="black")
frame1 = tk.Frame(window, width=90, bg="orange")
# frame1.pack(fill=tk.Y, side=tk.LEFT)
frame1.grid(row=0, column=0)
frame2 = tk.Frame(window, width=1890, bg="black")
# frame2.pack(fill=tk.Y, side=tk.RIGHT)
frame2.grid(row=0, column=1)
lotomat = tk.Button(frame1, text=" Start\n Lotomat", padx=10, pady=5, bg="orange", fg="black",
relief=tk.GROOVE, command=lambda : startLotomat())
# lotomat.pack()
lotomat.grid(row=1, column=0)
convert = tk.Button(frame1, text=" URL2IP \n on \n Desktop", padx=10, pady=5, bg="orange", fg="black",
relief=tk.GROOVE, command=lambda : startURL2IP())
# convert.pack()
convert.grid(row=2, column=0)
startRps = tk.Button(frame1, text=" Start \nR.P.S", padx=12, pady=5, bg="orange", fg="black",
relief=tk.GROOVE, command=lambda : startRPS())
# startRps.pack()
startRps.grid(row=3, column=0)
endRun = tk.Button(frame1, text="Quit", padx=12, pady=10, bg="orange", fg="black",
relief=tk.RIDGE, command=lambda : ending())
# endRun.pack()
endRun.grid(row=4, column=0)
def startLotomat():
os.system('python lotomat.py')
def startURL2IP():
os.system('python urltoipondesktop.py')
def startRPS():
os.system('python rockpaperscissors.py')
def ending():
exit()
window.mainloop()
maingui()
each button runs a different .py file.
how can I use frames to split the window so the program runs on the right side?
Thanks!
Edit:
I've added a pic of the GUI, the goal is to run the left menu buttons on the black frame.
Following acw1668's comment and alot of thread researching here's the thread solution. so elegant, so simple! I love python!
def thread_handler(self, host):
wt = threading.Thread(target=self.write_to_file, args=(host,))
pt = threading.Thread(target=self.print_to_box, args=(host,))
dt = threading.Thread(target=self.delete_default_lines, args=())
wt.start()
pt.start()
dt.start()
I have been doing a python app for a calculator with Tkinter. I want to know how access the buttons properties such as their size, height, width and color by the use of strings. So far I have been able to change the background of the lines behind the buttons to red by using style.configure("TButton", background='red',
font="Serif 15",
padding=10 ).
How do I change the buttons themselves? If anyone could help me I would greatly appreciate it.
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
class Calculator:
calc_value = 0.0
div_trigger = False
mult_trigger = False
add_trigger = False
sub_trigger = False
def __init__(self,root):
self.entry_value = StringVar(root,value="")
root.title("Calculator")
root.geometry("600x500")
root.resizable(width=True, height=True)
style = ttk.Style()
style.configure(self, background='red')
style.configure("TButton", #background='red',
font="Serif 15",
padding=10 )
style.configure("TEntry",
font="Serif 15",
padding=10 )
self.number_entry = ttk.Entry(root,
textvariable=self.entry_value,width=50)
self.number_entry.grid(row=0, columnspan=4)
#-------1st row---------
self.button7 = ttk.Button(root, text="7",
command=lambda: self.button_press('7')).grid(row=1, column=0)
self.button8 = ttk.Button(root, text="8",
command=lambda: self.button_press('8')).grid(row=1, column=1)
self.button9 = ttk.Button(root, text="9",
command=lambda: self.button_press('9')).grid(row=1, column=2)
self.button_div = ttk.Button(root, text="/",
command=lambda: self.button_press('/')).grid(row=1, column=3)
#-------2nd row---------
self.button4 = ttk.Button(root, text="4",
command=lambda: self.button_press('4')).grid(row=2, column=0)
self.button5 = ttk.Button(root, text="5",
command=lambda: self.button_press('5')).grid(row=2, column=1)
self.button6 = ttk.Button(root, text="6",
command=lambda: self.button_press('6')).grid(row=2, column=2)
self.button_mult = ttk.Button(root, text="*",
command=lambda: self.button_press('*')).grid(row=2, column=3)
#-------3rd row---------
self.button1 = ttk.Button(root, text="1",
command=lambda: self.button_press('1')).grid(row=4, column=0)
self.button2 = ttk.Button(root, text="2",
command=lambda: self.button_press('2')).grid(row=4, column=1)
self.button3 = ttk.Button(root, text="3",
command=lambda: self.button_press('3')).grid(row=4, column=2)
self.button_add = ttk.Button(root, text="+",
command=lambda: self.button_press('+')).grid(row=4, column=3)
#-------4th row---------
self.button_clear = ttk.Button(root, text="AC",
command=lambda: self.button_press('AC')).grid(row=5, column=0)
self.button0 = ttk.Button(root, text="0",
command=lambda: self.button_press('0')).grid(row=5, column=1)
self.button_equal = ttk.Button(root, text="=",
command=lambda: self.button_press('=')).grid(row=5, column=2)
self.button_sub = ttk.Button(root, text="-",
command=lambda: self.button_press('-')).grid(row=5, column=3)
root = Tk()
calc = Calculator(root)
root.mainloop()
#button1 = Button(topframe,padx=16, pady=16, bd=8, text="1", fg="black", bg = random.choice(colors))
Here are documentatiopn about that.
http://effbot.org/tkinterbook/button.htm
If you want to change a botton size you can use.
button1.config(height = 100, width = 100)
This question already exists:
"Unexpected EOF while parsing" in calculator
Closed 8 years ago.
I have a calculator in Python 3 but for some reason my code doesn't work. It works fine until I press the "=" button to calculate my thing (using tkinter for the gui). Here is my code (it is quite long, sorry for that, it is just a part from my code)
from tkinter import *
from tkinter.ttk import *
def SMATH():
#PUT TEXT IN ENTRY FUNCTION
def puttext(text):
sm_ent.insert(0,text)
return
#CLEAR THE ENTRY FUNCTION
def cleartext():
sm_ent.delete(0, END)
return
#GET THE OUTCOME FUNCTION
def Coutcome():
calc = caltext.get()
answer = eval(calc)
sm_ent = Entry(e_frame, textvariable=caltext, text=answer)
smathW = Tk()
smathW.title("Simple Math")
smathW.resizable(0,0)
smathW.wm_iconbitmap('icon.ico')
def quitSmath():
smathW.destroy()
#ENTRY FRAME
e_frame = Frame(smathW)
e_frame.pack()
#BUTTONS FRAME
b_frame = Frame(smathW)
b_frame.pack()
#ENTRY
caltext= StringVar()
sm_ent = Entry(e_frame, textvariable=caltext)
sm_ent.pack(fill=Y)
#LABEL
sm_inf = Label(smathW, text="Here is the Simple Math mode from CalfoWin")
sm_inf.pack()
#BUTTON 7
sm_butt0 = Button(b_frame, text="7", command=lambda: puttext('7'))
sm_butt0.grid(row=1, column=0)
#BUTTON 8
sm_butt1 = Button(b_frame, text="8", command=lambda: puttext('8'))
sm_butt1.grid(row=1, column=1)
#BUTTON 9
sm_butt2 = Button(b_frame, text="9", command=lambda: puttext('9'))
sm_butt2.grid(row=1, column=2)
#BUTTON 4
sm_butt3 = Button(b_frame, text="4", command=lambda: puttext('4'))
sm_butt3.grid(row=2, column=0)
#BUTTON 5
sm_butt4 = Button(b_frame, text="5", command=lambda: puttext('5'))
sm_butt4.grid(row=2, column=1)
#BUTTON 6
sm_butt5 = Button(b_frame, text="6", command=lambda: puttext('6'))
sm_butt5.grid(row=2, column=2)
#BUTTON 1
sm_butt6 = Button(b_frame, text="1", command=lambda: puttext('1'))
sm_butt6.grid(row=3, column=0)
#BUTTON 2
sm_butt7 = Button(b_frame, text="2", command=lambda: puttext('2'))
sm_butt7.grid(row=3, column=1)
#BUTTON 3
sm_butt8 = Button(b_frame, text="3", command=lambda: puttext('3'))
sm_butt8.grid(row=3, column=2)
#BUTTON 0
sm_butt9 = Button(b_frame, text="0", command=lambda: puttext('0'))
sm_butt9.grid(row=4, column=0)
#BUTTON KOMMA
sm_buttKomma = Button(b_frame, text=".", command=lambda: puttext('.'))
sm_buttKomma.grid(row=4, column=1)
#BUTTON EQUALSIGN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
sm_buttEsign = Button(b_frame, text="=", command=Coutcome())
sm_buttEsign.grid(row=4, column=2)
#BUTTON ADDITION
sm_buttPlus = Button(b_frame, text="+", command=lambda: puttext('+'))
sm_buttPlus.grid(row=2, column=3)
#BUTTON SUBTRACKTION
sm_buttMin = Button(b_frame, text="-", command=lambda: puttext('-'))
sm_buttMin.grid(row=3, column=3)
#BUTTON DIVISION
sm_buttDiv = Button(b_frame, text="/", command=lambda: puttext('/'))
sm_buttDiv.grid(row=3, column=4)
#BUTTON MULTIPLICATION
sm_buttMult = Button(b_frame, text='x', command=lambda: puttext('*'))
sm_buttMult.grid(row=2, column=4)
#BUTTON CLEAR
sm_buttClear = Button(b_frame, text="Clear", command=lambda: cleartext())
sm_buttClear.grid(row=1, column=3)
#BUTTON QUIT
sm_buttquit = Button(b_frame, text="Quit", command=lambda: quitSmath())
sm_buttquit.grid(row=1, column=4)
You use command = Coutcome() which passes the return value of Coutcome as the funtion to call when the button is pressed. So what happens now is that Coutcome is called when the button is created, and caltext is still empty. This means that you are running eval(''), which gives you the error you are seeing.
This is not what you want, you want Coutcome to be called when the button is pressed. So You should pass the function name, without parentheses, as the command
sm_buttEsign = Button(b_frame, text="=", command=Coutcome)
For your quit and clear buttons you should do the same thing, get rid of the parentheses and the lambda. The reason you need to use lambda sometimes is because when you have to leave out the parentheses, you can't pass variables to the command. When you don't need to pass variables, the lambda is superfluous.
In addition, you should be careful using eval. With the wrong input, it can be dangerous. So watch out with letting users input stuff to eval without proper restrictions.