I have two files in my directory (first.py and second.py). The first.py has a button. So on clicking the button in first.py gui window, it should be directed to second.py gui window.
first.py window photo and second.py window photo. So on clicking the sign up button in the first.py, it should go to the sign up page in second.py.
How to do the connection or linking between the two scripts?
first.py
import tkinter as tk
root=tk.Tk()
root.title("My Bank")
root.geometry("500x500")
photo=tk.PhotoImage(file="image1.gif")
label = tk.Label(root, image=photo)
label.image = photo
label.pack()
label.place(x=0, y=0, relwidth=1, relheight=1)
tfm = tk.Frame(root, width=2000, height=50)
tfm.pack(side=tk.TOP)
w = tk.Label(tfm, text="MY bank", font=("Times", "24", "bold"), bg="yellow", anchor="e", fg="black", padx=350, pady=10)
w.pack(fill="both")
bfm = tk.Frame(root, width=2000, height=50, bg="gray")
bfm.pack(side=tk.BOTTOM)
w = tk.Label(root, text="Main Menu", font=("Times", "24", "bold"), bg="black", fg="white", padx=350, pady=10)
w.pack(padx=10, pady=30)
button1 = tk.Button(root, text="Sign Up", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button1.pack(padx=10, pady=10)
button2 = tk.Button(root, text="Sign In", width=12, height=1,
bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button2.pack(padx=10, pady=10)
button3 = tk.Button(root, text="Admin Sign In", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button3.pack(padx=10, pady=10)
button4 = tk.Button(root, text="Quit!", width=5, height=1, bg="black",fg="white", bd="10", font=("Helvetica", "12", "bold"))
button4.pack(padx=10, pady=10)
root.mainloop()
second.py
import tkinter as tk
root=tk.Tk()
root.title("My Bank")
root.geometry("500x500")
photo=tk.PhotoImage(file="image1.gif")
label = tk.Label(root, image=photo)
label.image = photo
label.pack()
label.place(x=0, y=0, relwidth=1, relheight=1)
tfm = tk.Frame(root, width=2000, height=50)
tfm.pack(side=tk.TOP)
w = tk.Label(tfm, text="MY bank", font=("Times", "24", "bold"), bg="yellow", anchor="e", fg="black", padx=350, pady=10)
w.pack(fill="both")
bfm = tk.Frame(root, width=2000, height=50, bg="gray")
bfm.pack(side=tk.BOTTOM)
w = tk.Label(root, text="Sign Up", font=("Times", "24", "bold"), bg="black", fg="white", padx=350, pady=10)
w.pack(padx=10, pady=30)
e1 = tk.Entry(root, width=20, font=("Times", "14", "bold"), bd=3, fg="blue")
e1.insert(0, 'Username')
e1.pack(padx=150, pady=10)
e2 = tk.Entry(root, width=20, font=("Times", "14", "bold"), bd=3, fg="blue")
e2.insert(0, 'Email')
e2.pack(padx=150, pady=10)
e3 = tk.Entry(root, width=20, font=("Times", "14", "bold"), bd=3, fg="blue")
e3.insert(0, 'Password')
e3.pack(padx=150, pady=10)
button1 = tk.Button(root, text="Sign Up", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button1.pack(padx=100, pady=20)
root.mainloop()
I went through your problem and I guess you want to open another file when the button is clicked. To do this you need to import the file which you want to load on button click. And create another tkinter function in the external script. While running through your code i even came across the error which is tkinter.TclError: image "pyimage3" doesn't exist as of now I even fixed this for more information visit this link. Here is the code which i made all the changes.
"""
Spyder Editor
This is a temporary script file.
"""
import second
import tkinter as tk
root=tk.Toplevel()
root.title("My Bank")
root.geometry("500x500")
photo=tk.PhotoImage(file="image1.gif")
label = tk.Label(root, image=photo)
label.image = photo
label.pack()
label.place(x=0, y=0, relwidth=1, relheight=1)
tfm = tk.Frame(root, width=2000, height=50)
tfm.pack(side=tk.TOP)
w = tk.Label(tfm, text="MY bank", font=("Times", "24", "bold"), bg="yellow", anchor="e", fg="black", padx=350, pady=10)
w.pack(fill="both")
bfm = tk.Frame(root, width=2000, height=50, bg="gray")
bfm.pack(side=tk.BOTTOM)
w = tk.Label(root, text="Main Menu", font=("Times", "24", "bold"), bg="black", fg="white", padx=350, pady=10)
w.pack(padx=10, pady=30)
button1 = tk.Button(root, text="Sign Up", command=lambda : second.signup() , width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button1.pack(padx=10, pady=10)
button2 = tk.Button(root, text="Sign In", width=12, height=1,
bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button2.pack(padx=10, pady=10)
button3 = tk.Button(root, text="Admin Sign In", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button3.pack(padx=10, pady=10)
button4 = tk.Button(root, text="Quit!", width=5, height=1, bg="black",fg="white", bd="10", font=("Helvetica", "12", "bold"))
button4.pack(padx=10, pady=10)
root.mainloop()
and the other one
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 7 11:09:28 2018
#author: kedar
"""
import tkinter as tk
def signup():
root=tk.Toplevel()
root.title("My Bank")
root.geometry("500x500")
photo=tk.PhotoImage(file="image1.gif")
label = tk.Label(root, image=photo)
label.image = photo
label.pack()
label.place(x=0, y=0, relwidth=1, relheight=1)
tfm = tk.Frame(root, width=2000, height=50)
tfm.pack(side=tk.TOP)
w = tk.Label(tfm, text="MY bank", font=("Times", "24", "bold"), bg="yellow", anchor="e", fg="black", padx=350, pady=10)
w.pack(fill="both")
bfm = tk.Frame(root, width=2000, height=50, bg="gray")
bfm.pack(side=tk.BOTTOM)
w = tk.Label(root, text="Sign Up", font=("Times", "24", "bold"), bg="black", fg="white", padx=350, pady=10)
w.pack(padx=10, pady=30)
e1 = tk.Entry(root, width=20, font=("Times", "14", "bold"), bd=3, fg="blue")
e1.insert(0, 'Username')
e1.pack(padx=150, pady=10)
e2 = tk.Entry(root, width=20, font=("Times", "14", "bold"), bd=3, fg="blue")
e2.insert(0, 'Email')
e2.pack(padx=150, pady=10)
e3 = tk.Entry(root, width=20, font=("Times", "14", "bold"), bd=3, fg="blue")
e3.insert(0, 'Password')
e3.pack(padx=150, pady=10)
button1 = tk.Button(root, text="Sign Up", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button1.pack(padx=100, pady=20)
root.mainloop()
For now you can just copy this and use it. I hope it helps.
Related
I am coding a automation interface using tkinter and i have to import a second py file from the first by press of a button, the importing works but i am faced with an error that says "image "pyimage1" doesn't exist", here is the code for the main interface"
import tkinter as tk1
window=tk1.Tk()
window.wm_title("Main Interface")
def Function():
import What
#main
Canvas= tk1.Canvas(window, height=1000, width=1000)
Canvas.grid(row=30, column=20)
Label1=tk1.Label(Canvas, text="WELCOME TO DIGITAL AUTOMATION", bg="#eddea8", font="Verdana 34", relief="solid", borderwidth=5)
Label1.grid(row=0, column=1)
Label1=tk1.Label(Canvas, text="What do you want to perform ? ", bg="#eddea8", font="Verdana 18", relief="solid", borderwidth=5)
Label1.grid(row=1, column=1)
Label2=tk1.Label(Canvas, text="Schedule A message ?", bg="#33CCFF", font="Verdana 12")
Label2.grid(row=2, column=0)
Button1=tk1.Button(Canvas, bg="#59cced", text="Press Here", font="Verdana 18", command=Function)
Button1.grid(row=3, column=0)
window.mainloop()
----------and here is the code for the whatsapp automation---------------------------------
from tkinter import *
import pywhatkit
#import pkg_resources.py2_warn
root = Tk()
root.wm_iconbitmap("icon/default.ico")
root.wm_title("Mscheduler")
bg = PhotoImage(file = "1187248.png")
#root.geometry("965x250")
#Function
def Function():
n=Entry1.get()
m=Entry2.get()
L=Entry3.get()
I=Entry4.get()
y="+91"
pywhatkit.sendwhatmsg(y+n,str(m),int(L),int(I))
Entry1.delete(0,END)
Entry2.delete(0,END)
Entry3.delete(0,END)
Entry4.delete(0,END)
#def delete():
#Entry1.delete(0,END)
#mainscreen
Canvas = Canvas(root, height=1000, width=1000)
Canvas.grid(row=30, column=20)
Canvas.create_image( 0, 0, image = bg, anchor = "nw")
Label1 = Label(Canvas, text="Mscheduler",bg='#99e6ff',font="Verdana 34", relief="solid", borderwidth=5)
Label1.grid(row=0,column=1)
Label2= Label(Canvas, bg="#A9ECFF", text="Phone no:",font="verdana 18")
Label2.grid(row=2 , column=0)
#Canvas.create_text(105, 115, text="Phone No.:",font="verdana 18")
#Canvas.create_text.grid(row=2 , column=0)
Entry1=Entry(Canvas,bg="#ffe6b3",font="verdana 18", relief="ridge", borderwidth=3)
Entry1.grid(row=2, column=1)
Label3= Label(Canvas,bg="#A9ECFF", text=" Message:",font="verdana 18")
Label3.grid(row=3 , column=0)
#Canvas.create_text(115, 170, text="Message:",font="verdana 18")
Entry2=Entry(Canvas,bg="#ffe6b3",font="verdana 18", relief="ridge", borderwidth=3)
Entry2.grid(row=3, column=1)
Label4= Label(Canvas,bg="#A9ECFF", text=" Hour:",font="verdana 18")
Label4.grid(row=4 , column=0)
#Canvas.create_text(135, 225, text=" Hour:",font="verdana 18")
Entry3=Entry(Canvas,bg="#ffe6b3",font="verdana 18", relief="ridge", borderwidth=3)
Entry3.grid(row=4, column=1)
Label5= Label(Canvas, bg="#A9ECFF", text=" Minutes:",font="verdana 18")
Label5.grid(row=5 , column=0)
#Canvas.create_text(135,225 text="Minutes:", font="verdana 18")
#Canvas.create_text(200, 350, text="Phone No.:",font="verdana 18")
Entry4=Entry(Canvas,bg="#ffe6b3",font="verdana 18", relief="ridge", borderwidth=3)
Entry4.grid(row=5, column=1)
Button1=Button(Canvas, bg="#d1d1e0", text="send --->", font="verdana 18", borderwidth=3, command=Function)
Button1.grid(row=7, column=2)
#Button1=Button(Canvas, bg="#d1d1e0", text="delete", font="verdana 18", borderwidth=5, command=delete)
#Button1.grid(row=7, column=1)
root.mainloop()
i tried using tk.Toplevel and all but it doesnt seem to work, please help
This task does not require the use of two instances of Tk. You should read about splitting a program into modules and about naming variables in python. The sample program consists of two files main.py and whatsappauto.py. I didn't import some libraries to make it easier to show how to work with two modules.
main.py
import tkinter as tk
from functools import partial
import whatsappauto # Importing a second file
window=tk.Tk()
window.wm_title("Main Interface")
def Function(window):
# Calling a function from another file
whatsappauto.top_level(window)
#main
canvas= tk.Canvas(window, height=1000, width=1000)
canvas.grid(row=30, column=20)
Label1=tk.Label(canvas, text="WELCOME TO DIGITAL AUTOMATION", bg="#eddea8", font="Verdana 34", relief="solid", borderwidth=5)
Label1.grid(row=0, column=1)
Label1=tk.Label(canvas, text="What do you want to perform ? ", bg="#eddea8", font="Verdana 18", relief="solid", borderwidth=5)
Label1.grid(row=1, column=1)
Label2=tk.Label(canvas, text="Schedule A message ?", bg="#33CCFF", font="Verdana 12")
Label2.grid(row=2, column=0)
Button1=tk.Button(canvas, bg="#59cced", text="Press Here", font="Verdana 18", command=partial(Function, window))
Button1.grid(row=3, column=0)
window.mainloop()
whatsappauto.py
import tkinter as tk
from functools import partial
def Function(Entry1, Entry2, Entry3, Entry4):
n = Entry1.get()
m = Entry2.get()
L = Entry3.get()
I = Entry4.get()
y = "+91"
# pywhatkit.sendwhatmsg(y + n, str(m), int(L), int(I))
Entry1.delete(0, tk.END)
Entry2.delete(0, tk.END)
Entry3.delete(0, tk.END)
Entry4.delete(0, tk.END)
def top_level(root):
global canvas_bg
newWindow1 = tk.Toplevel(root)
newWindow1.wm_iconbitmap("icon/default.ico")
newWindow1.wm_title("Mscheduler")
canvas = tk.Canvas(newWindow1)
canvas.pack()
canvas_bg = tk.PhotoImage(file='1187248.png')
canvas.create_image(0, 0, image=canvas_bg, anchor="nw")
Label1 = tk.Label(canvas, text="Mscheduler", bg='#99e6ff', font="Verdana 34", relief="solid", borderwidth=5)
Label1.grid(row=0, column=1)
Label2 = tk.Label(canvas, bg="#A9ECFF", text="Phone no:", font="verdana 18")
Label2.grid(row=2, column=0)
Entry1 = tk.Entry(canvas, bg="#ffe6b3", font="verdana 18", relief="ridge", borderwidth=3)
Entry1.grid(row=2, column=1)
Label3 = tk.Label(canvas, bg="#A9ECFF", text=" Message:", font="verdana 18")
Label3.grid(row=3, column=0)
# Canvas.create_text(115, 170, text="Message:",font="verdana 18")
Entry2 = tk.Entry(canvas, bg="#ffe6b3", font="verdana 18", relief="ridge", borderwidth=3)
Entry2.grid(row=3, column=1)
Label4 = tk.Label(canvas, bg="#A9ECFF", text=" Hour:", font="verdana 18")
Label4.grid(row=4, column=0)
Entry3 = tk.Entry(canvas, bg="#ffe6b3", font="verdana 18", relief="ridge", borderwidth=3)
Entry3.grid(row=4, column=1)
Label5 = tk.Label(canvas, bg="#A9ECFF", text=" Minutes:", font="verdana 18")
Label5.grid(row=5, column=0)
Entry4 = tk.Entry(canvas, bg="#ffe6b3", font="verdana 18", relief="ridge", borderwidth=3)
Entry4.grid(row=5, column=1)
Button1 = tk.Button(canvas, bg="#d1d1e0", text="send --->", font="verdana 18",
borderwidth=3, command=partial(Function, Entry1, Entry2, Entry3, Entry4))
Button1.grid(row=7, column=2)
I have a problem with a button (Clear) I tried a lot of ways and it did not work and I search about this problem but I did not find the solution to my problem, so I want to clear the contents from the entry when I press on the button Clear.
previous, from the ways I tried it :
entry.delete(0, END)
entry.delete(0, 'end')
entry.insert(index, " ")
and a lot of ways, so if you have the solution give me that, and thank you a lot in advanced
and this is my code:
from tkinter import *
from tkinter import ttk
import pyperclip as clip
root = Tk()
root.geometry('350x500')
root.title("Calculator")
root.configure(bg="#2D3A3E")
root.resizable(False, False)
largeFont = ('Comic Sans MS', 20)
fontButton = ('Comic Sans MS', 20)
style = ttk.Style()
i = 0
def press(x):
global i
i += 1
entry.configure(state=NORMAL)
entry.insert(i, x) # المشكلة هنا أننا عندما نستخدم insert(index, value
entry.configure(state=DISABLED)
def equalResult():
pass
def copyNum():
getText = entry.get()
clip.copy(getText)
clip.paste()
root.update()
def clear():
entry.delete(0, END)
# create buttons and entry
#('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
entry = Entry(root, textvariable=largeFont, font=largeFont, bg='#2D3A3E', fg="#D6DFE0")
entry.place(width=400, height=100, y=0)
#All Buttons here 19:
style.theme_use('alt')
style.configure("TButton", foreground="#D6DFE0", font=fontButton, background='#2D3A3E')
zero = ttk.Button(root, text='0', command=lambda: press(0))
zero.place(width=75, height=60, y=440, x=0)
dot = ttk.Button(root, text='.', command=lambda: press('.'))
dot.place(width=75, height=60, y=440, x=75)
one = ttk.Button(root, text='1', command=lambda: press(1))
one.place(width=75, height=60, y=380, x=0)
two = ttk.Button(root, text='2', command=lambda: press(2))
two.place(width=75, height=60, y=380, x=75)
three = ttk.Button(root, text='3', command=lambda: press(3))
three.place(width=75, height=60, y=380, x=150)
four = ttk.Button(root, text='4', command=lambda: press(4))
four.place(width=75, height=60, y=320, x=0)
five = ttk.Button(root, text='5', command=lambda: press(5))
five.place(width=75, height=60, y=320, x=75)
six = ttk.Button(root, text='6', command=lambda: press(6))
six.place(width=75, height=60, y=320, x=150)
seven = ttk.Button(root, text='7', command=lambda: press(7))
seven.place(width=75, height=60, y=260, x=0)
eight = ttk.Button(root, text='8', command=lambda: press(8))
eight.place(width=75, height=60, y=260, x=75)
nine = ttk.Button(root, text='9', command=lambda: press(9))
nine.place(width=75, height=60, y=260, x=150)
equal = Button(root, text='=', fg="#D6DFE0", bg="green", font=("Comic Sans MS", 30, 'bold'),
command=lambda: press('='))
equal.place(width=200, height=60, y=440, x=150)
plus = Button(root, text='+', fg="#D6DFE0", bg="green", font=("Comic Sans MS", 30,'bold'),
command=lambda: press('+'))
plus.place(width=125, height=60, y=380, x=225)
minus = Button(root, text='ـــ', fg="#D6DFE0", bg="green", font=("arial", 30,'bold'),
command=lambda: press('-'))
minus.place(width=125, height=60, y=320, x=225)
multiply = Button(root, text='x', fg="#D6DFE0", bg="green", font=("arial", 30, 'bold'),
command=lambda: press('*'))
multiply.place(width=125, height=60, y=260, x=225)
mod = ttk.Button(root, text='%', command=lambda: press('%'))
mod.place(width=75, height=60, y=200, x=150)
# btn clear all text in the entry
Clear = ttk.Button(root, text='C', command=lambda: clear())
Clear.place(width=75, height=60, y=200, x=75)
# button remove text from the end
remove = ttk.Button(root, text='rem')
remove.place(width=75, height=60, y=200, x=0)
divide = Button(root, text='/', fg="#D6DFE0", bg="green", font=("arial", 30,'bold'),
command=lambda: press('/'))
divide.place(width=125, height=60, y=200, x=225)
# btn color mode
colorMode = Button(root, text='color mode', fg="#D6DFE0", bg="orange", font=("arial",
25,'bold'))
colorMode.place(width=225, height=40, y=160, x=1)
# btn copy
Copy = Button(root, text='copy', fg="#D6DFE0", bg="GREEN", font=("arial", 15,'bold'),
command=lambda: copyNum())
Copy.place(width=125, height=40, y=160, x=225)
entry.configure(state=DISABLED)
mainloop()
You aren't re enabling that entry. You had disabled that entry from here and not enabling. So, you are facing this issue:
def clear():
entry.configure(state=NORMAL)
entry.delete(0, END)
entry.configure(state=DISABLED)
Doing this should solve your problem.
And additionally you don't have to use other library just to copy text from entry, you can use this:
def copy_button():
clip = Tk()
clip.withdraw()
clip.clipboard_clear()
entry.configure(state=NORMAL)
text=entry.get()
entry.configure(state=DISABLED)
clip.clipboard_append(text)
clip.destroy()
I am writing my first "big" project - simple calculator with Tkinter UI. Having done the visual i got a problem of sending the digit/math operators to the screen of the calculator. When i try to send the digit into the adder() via argument it doesn't do anything (adder is a function that creates string that will be sent to the screen and math solver). def eq is not copleted but the screen should work even w/o it. help pls
By the way i can't get why screen_out isn't seem inside the adder (that's why i use global)
The code:
from tkinter import *
# logical part
screen_out = ''
def adder(ele):
global screen_out
screen_out += str(ele)
screen.configure(text=f"{screen_out}")
def deleter():
global screen_out
screen_out = screen_out[:-1]
screen.configure(text=f"{screen_out}")
def cleaner():
global screen_out
screen_out = ''
screen.configure(text=f"{screen_out}")
def eq():
global task
task = int(screen_out)
# main window
window = Tk()
window.title('Basic Calculator')
window.geometry('400x350')
# output screen
black_lbl = Label(window, width=1, height=2, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8')
black_lbl.place(x=390, y=0)
screen_former = Label(window, text="", width=6, height=2, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8')
screen_former.grid(column=0, row=0)
screen = Label(window, text="", width=27, height=2, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', anchor="e")
screen.place(x=-21, y=0)
# keyboard interface
btn9 = Button(window, text="9", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=adder('9'))
btn8 = Button(window, text="8", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('8'))
btn7 = Button(window, text="7", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('7'))
btn6 = Button(window, text="6", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('6'))
btn5 = Button(window, text="5", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('5'))
btn4 = Button(window, text="4", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('4'))
btn3 = Button(window, text="3", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('3'))
btn2 = Button(window, text="2", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('2'))
btn1 = Button(window, text="1", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('1'))
btn0 = Button(window, text="0", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('0'))
btn_dot = Button(window, text=".", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('.'))
btn_eq = Button(window, text="=", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=eq)
btn_plus = Button(window, text="+", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('+'))
btn_minus = Button(window, text="-", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('-'))
btn_mult = Button(window, text="*", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('*'))
btn_div = Button(window, text="/", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('/'))
btn_par_open = Button(window, text="(", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('('))
btn_par_clos = Button(window, text=")", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder(')'))
btn_clear = Button(window, text="C", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=cleaner)
btn_del = Button(window, text="DEL", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=deleter)
u = 0
if u == 0:
cleaner()
u += 1
btn_par_open.grid(column=0, row=1)
btn_par_clos.grid(column=1, row=1)
btn_clear.grid(column=2, row=1)
btn_del.grid(column=3, row=1)
btn7.grid(column=0, row=2)
btn8.grid(column=1, row=2)
btn9.grid(column=2, row=2)
btn_plus.grid(column=3, row=2)
btn4.grid(column=0, row=3)
btn5.grid(column=1, row=3)
btn6.grid(column=2, row=3)
btn_minus.grid(column=3, row=3)
btn1.grid(column=0, row=4)
btn2.grid(column=1, row=4)
btn3.grid(column=2, row=4)
btn_mult.grid(column=3, row=4)
btn_eq.grid(column=0, row=5)
btn0.grid(column=1, row=5)
btn_dot.grid(column=2, row=5)
btn_div.grid(column=3, row=5)
window.mainloop()
You're calling the adder() function when you create the button. The command argument must be a function that will be called when the user clicks on the button. Use a lambda for this.
btn9 = Button(window, text="9", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=lambda: adder('9'))
Also, your other buttons have a typo: command=command=
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)
My code for my messagebox won't work on my window. Everything else works but when I run it my "About" messagebox won't show up. I want my messagebox to pop out when "About" is clicked on my window. What can I do to make it work?
from tkinter import *
from tkinter import messagebox
calculator = Tk()
calculator.title("Calculator")
calculator.geometry("317x145")
menubar = Menu(calculator)
class Calculator(Frame):
def __init__(self):
Frame.__init__(self)
display = Frame(calculator, bd=0, width=1000, height=1000, relief=SUNKEN)
buttons = Frame(calculator, bd=0, width=7, height=1, relief=GROOVE)
display.grid(column=0, row=0, padx=0, pady=0)
buttons.grid(column=0, row=1, padx=1)
numbers = StringVar()
self.results = Entry(display, textvariable=numbers, width=31, fg="DarkOrchid4", bg="lavender blush", font="Verdana")
self.results.pack()
self.results.grid(column=0, row=0)
def showup(x):
return lambda: self.results.insert(END, x)
numbers=["7", "4", "1", "8", "5", "2", "9", "6", "3"]
for i in range(9):
n=numbers[i]
Button(buttons, bg="snow", text=n, width=7, height=1, command=showup(n), relief=RAISED).grid(row=i%3, column=i//3)
Clear = Button(buttons, bg="snow", text="C", width=7, height=1, command=self.clear, relief=RAISED)
Clear.grid(padx=2, pady=2, column=3, row=0)
Equals = Button(buttons, bg="snow", text="=", width=7, height=1, command=self.equals, relief=RAISED)
Equals.grid(padx=2, pady=2, column=4, row=3)
All_clear = Button(buttons, bg="snow", text="AC", width=7, height=1, command=self.all_clear, relief=RAISED)
All_clear.grid(padx=2, pady=2, column=4, row=0)
Bracket_one = Button(buttons, bg="snow", text="(", width=7, height=1, command=self.bracket_one, relief=RAISED)
Bracket_one.grid(padx=2, pady=2, column=2, row=3)
Bracket_two = Button(buttons, bg="snow", text=")", width=7, height=1, command=self.bracket_two, relief=RAISED)
Bracket_two.grid(padx=2, pady=2, column=3, row=3)
Zero = Button(buttons, bg="snow", text="0", width=7, height=1, command=self.zero, relief=RAISED)
Zero.grid(padx=2, pady=2, column=0, row=3)
Decimal_point = Button(buttons, bg="snow", text=".", width=7, height=1, command=self.decimal_point, relief=RAISED)
Decimal_point.grid(padx=2, pady=2, column=1, row=3)
Multiplication = Button(buttons, bg="red", text="x", width=7, height=1, command=self.multiplication, relief=RAISED)
Multiplication.grid(padx=2, pady=2, column=3, row=1)
Division = Button(buttons, bg="powder blue", text="/", width=7, height=1, command=self.division, relief=RAISED)
Division.grid(padx=2, pady=2, column=4, row=1)
Addition = Button(buttons, bg="yellow", text="+", width=7, height=1, command=self.addition, relief=RAISED)
Addition.grid(padx=2, pady=2, column=3, row=2)
Subtraction = Button(buttons, bg="green", text="-", width=7, height=1, command=self.subtraction, relief=RAISED)
Subtraction.grid(padx=2, pady=2, column=4, row=2)
def equals(self):
try:
result = eval(self.results.get())
except:
result = "Invalid input"
self.all_clear()
self.results.insert(0, result)
def zero(self):
self.results.insert(END, "0")
def bracket_one(self):
self.results.insert(END, "(")
def bracket_two(self):
self.results.insert(END, ")")
def all_clear(self):
self.results.delete(0, END)
def clear(self):
self.results.delete(-1)
def multiplication(self):
self.results.insert(END, "*")
def division(self):
self.results.insert(END, "/")
def addition(self):
self.results.insert(END, "+")
def subtraction(self):
self.results.insert(END, "-")
def decimal_point(self):
self.results.insert(END, ".")
def about():
messagebox.showinfo(title = "About", message = "Author")
return
helpMenu = Menu(menubar)
menubar.add_command(label = "About", command=about)
if __name__ == '__main__':
Calculator().mainloop()
calculator.config(menu=menubar)
calculator.mainloop()
Your issue is that you are not associating the menubar with your root application. That is why it is never coming up. You need to configure the menu for your application to be the menubar you create. Example -
calculator.configure(menu=menubar)
Also, it would be better to move that code inside you frame as well. Example -
from tkinter import *
from tkinter import messagebox
calculator = Tk()
calculator.title("Calculator")
calculator.geometry("317x145")
menubar = Menu(calculator)
class Calculator(Frame):
def __init__(self):
Frame.__init__(self)
display = Frame(calculator, bd=0, width=1000, height=1000, relief=SUNKEN)
buttons = Frame(calculator, bd=0, width=7, height=1, relief=GROOVE)
display.grid(column=0, row=0, padx=0, pady=0)
buttons.grid(column=0, row=1, padx=1)
numbers = StringVar()
self.results = Entry(display, textvariable=numbers, width=31, fg="DarkOrchid4", bg="lavender blush", font="Verdana")
self.results.pack()
self.results.grid(column=0, row=0)
def showup(x):
return lambda: self.results.insert(END, x)
numbers=["7", "4", "1", "8", "5", "2", "9", "6", "3"]
for i in range(9):
n=numbers[i]
Button(buttons, bg="snow", text=n, width=7, height=1, command=showup(n), relief=RAISED).grid(row=i%3, column=i//3)
Clear = Button(buttons, bg="snow", text="C", width=7, height=1, command=self.clear, relief=RAISED)
Clear.grid(padx=2, pady=2, column=3, row=0)
Equals = Button(buttons, bg="snow", text="=", width=7, height=1, command=self.equals, relief=RAISED)
Equals.grid(padx=2, pady=2, column=4, row=3)
All_clear = Button(buttons, bg="snow", text="AC", width=7, height=1, command=self.all_clear, relief=RAISED)
All_clear.grid(padx=2, pady=2, column=4, row=0)
Bracket_one = Button(buttons, bg="snow", text="(", width=7, height=1, command=self.bracket_one, relief=RAISED)
Bracket_one.grid(padx=2, pady=2, column=2, row=3)
Bracket_two = Button(buttons, bg="snow", text=")", width=7, height=1, command=self.bracket_two, relief=RAISED)
Bracket_two.grid(padx=2, pady=2, column=3, row=3)
Zero = Button(buttons, bg="snow", text="0", width=7, height=1, command=self.zero, relief=RAISED)
Zero.grid(padx=2, pady=2, column=0, row=3)
Decimal_point = Button(buttons, bg="snow", text=".", width=7, height=1, command=self.decimal_point, relief=RAISED)
Decimal_point.grid(padx=2, pady=2, column=1, row=3)
Multiplication = Button(buttons, bg="red", text="x", width=7, height=1, command=self.multiplication, relief=RAISED)
Multiplication.grid(padx=2, pady=2, column=3, row=1)
Division = Button(buttons, bg="powder blue", text="/", width=7, height=1, command=self.division, relief=RAISED)
Division.grid(padx=2, pady=2, column=4, row=1)
Addition = Button(buttons, bg="yellow", text="+", width=7, height=1, command=self.addition, relief=RAISED)
Addition.grid(padx=2, pady=2, column=3, row=2)
Subtraction = Button(buttons, bg="green", text="-", width=7, height=1, command=self.subtraction, relief=RAISED)
Subtraction.grid(padx=2, pady=2, column=4, row=2)
self.menubar = Menu(self)
def about():
messagebox.showinfo(title = "About", message = "Author")
return
self.helpMenu = Menu(self.menubar)
self.menubar.add_cascade(label="Help",menu=self.helpMenu)
self.helpMenu.add_command(label = "About", command=about)
calculator.config(menu=self.menubar)
def equals(self):
try:
result = eval(self.results.get())
except:
result = "Invalid input"
self.all_clear()
self.results.insert(0, result)
def zero(self):
self.results.insert(END, "0")
def bracket_one(self):
self.results.insert(END, "(")
def bracket_two(self):
self.results.insert(END, ")")
def all_clear(self):
self.results.delete(0, END)
def clear(self):
self.results.delete(-1)
def multiplication(self):
self.results.insert(END, "*")
def division(self):
self.results.insert(END, "/")
def addition(self):
self.results.insert(END, "+")
def subtraction(self):
self.results.insert(END, "-")
def decimal_point(self):
self.results.insert(END, ".")
if __name__ == '__main__':
Calculator().mainloop()
calculator.config(menu=menubar)
calculator.mainloop()