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()
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)
Python TKinter Mac: Buttons layout strangely and when a button is clicked it looks the same (as if it hadn't been clicked) however I didn't check to see if the button worked.
and the space is a textbook
the Mac version is: a 2017 iMac running Monterey 12.4
import tkinter as tk
window = tk.Tk()
window.geometry("500x500")
window.title("Stock Watchlist & Logger")
label = tk.Label(window, text="Tester", font=('Ariel', 18))
label.pack(padx=20, pady=20)
textbox = tk.Text(window, height=3, font=('Ariel', 16))
textbox.pack(padx=10, pady=10)
numbuttframe = tk.Frame(window)
numbuttframe.columnconfigure(0, weight=1)
yesnobuttframe = tk.Frame(window)
yesnobuttframe.columnconfigure(1, weight=1)
button1 = tk.Button(numbuttframe, text="1", font=('Arial', 18))
button1.grid(row=0, column=0, sticky=tk.W+tk.E)
button2 = tk.Button(numbuttframe, text="2", font=('Arial', 18))
button2.grid(row=0, column=1, sticky=tk.W+tk.E)
button3 = tk.Button(numbuttframe, text="3", font=('Arial', 18))
button3.grid(row=0, column=2, sticky=tk.W+tk.E)
button4 = tk.Button(numbuttframe, text="4", font=('Arial', 18))
button4.grid(row=0, column=3, sticky=tk.W+tk.E)
button5 = tk.Button(numbuttframe, text="5", font=('Arial', 18))
button5.grid(row=0, column=4, sticky=tk.W+tk.E)
button6 = tk.Button(numbuttframe, text="6", font=('Arial', 18))
button6.grid(row=0, column=5, sticky=tk.W+tk.E)
yesbutton = tk.Button(yesnobuttframe, text="Yes", font=('Arial', 18))
yesbutton.grid(row=0, column=0, sticky=tk.W+tk.E)
nobutton = tk.Button(yesnobuttframe, text="No", font=('Arial', 18))
nobutton.grid(row=0, column=1, sticky=tk.W+tk.E)
numbuttframe.pack(fill='x')
yesnobuttframe.pack(fill='x')
if button1:
print("CELEBRATE!")
window.mainloop()
You set the weight to two cells weight=1 , so they expanded, since the rest have a default weight of 0. Since I don’t see your grid - the buttons are placed in frames one after the other, suggest using the pack method. And shortened your code a bit. The buttons won't work yet, because they don't have a command = call. Complex variable names in Python are usually written with an underscore.
import tkinter as tk
FONT = ('Ariel', 18)
window = tk.Tk()
window.geometry("500x500")
window.title("Stock Watchlist & Logger")
lbl = tk.Label(window, text="Tester", font=FONT)
lbl.pack(padx=20, pady=20)
text_box = tk.Text(window, height=3, font=('Ariel', 16))
text_box.pack(padx=10, pady=10)
num_btn = tk.Frame(window)
yes_no_btn = tk.Frame(window)
num_btn.pack(fill='x')
yes_no_btn.pack(fill='x')
buttons = []
for i in range(1, 7):
btn = tk.Button(num_btn, text=f"{i}", font=FONT)
btn.pack(side="left", fill='x', expand=True)
buttons.append(btn)
yes_btn = tk.Button(yes_no_btn, text="Yes", font=FONT)
yes_btn.pack(side="left", fill='x', expand=True)
no_btn = tk.Button(yes_no_btn, text="No", font=FONT)
no_btn.pack(side="left", fill='x', expand=True)
window.mainloop()
I have made a python gui using tkinter and i have been running it through the commandline. But when i tried to open it by double clicking, it shortly shows the window but exits again. I tried commenting out all the places where it uses images thinking that it is because it dosent have file-editing permissions when its run by double click and it worked! But i need to use images in the gui, anyone have solutions?
import tkinter as tk
from PIL import Image, ImageTk
import time
root = tk.Tk()
root.title("Krypto Coin")
root.iconbitmap('icon.ico')
root.configure(bg="#112233")
Frame = tk.Frame(root, width=1200, height=800, bg="#112233")
Frame.grid(columnspan=3, rowspan=5)
icon = Image.open('icon_round.png')
icon = ImageTk.PhotoImage(icon)
icon_label = tk.Label(image=icon, borderwidth=0, highlightthickness=0)
icon_label.place(relx=0.5, rely=0.2, anchor="center")
text = tk.Label(root, text="Welcome to Krypto Coin (KTC)", bg="#112233", fg="#ffffff", font=("Arial", 32))
text.place(relx=0.5, rely=0.45, anchor="center")
text = tk.Label(root, text="Enter id of the receiver and amount", bg="#112233", fg="#ffffff", font=("Arial", 24))
text.place(relx=0.5, rely=0.5, anchor="center")
def trasfer_button_pressed():
time.sleep(0.1)
trasfer_button_text.set("Loading...")
trasfer_button.configure(state="disable")
trasfer_button.configure(bg="#112233")
input_id.configure(state="disable")
input_amount.configure(state="disable")
def trasfer_button_enter(e):
if trasfer_button_text.get() == "Transfer":
trasfer_button.configure(bg="#334455")
def trasfer_button_leave(e):
if trasfer_button_text.get() == "Transfer":
trasfer_button.configure(bg="#223344")
trasfer_button_text = tk.StringVar()
trasfer_button = tk.Button(root, textvariable=trasfer_button_text, command=lambda:trasfer_button_pressed(), bg="#223344", fg="#ffffff", width=15, height=1, font=("Arial", 24), borderwidth=0, highlightthickness=0)
trasfer_button_text.set("Transfer")
trasfer_button.place(relx=0.5, rely=0.65, anchor="center")
trasfer_button.bind("<Enter>", trasfer_button_enter)
trasfer_button.bind("<Leave>", trasfer_button_leave)
input_id = tk.Entry(root, width=48, font = "Arial 14", bg="#334455", fg="#ffffff", borderwidth=0, highlightthickness=0)
input_id.insert(0, "ID")
input_id.place(relx=0.45, rely=0.8, anchor="center", height=30)
input_amount = tk.Entry(root, width=20, font = "Arial 14", bg="#334455", fg="#ffffff", borderwidth=0, highlightthickness=0)
input_amount.insert(0, "Amount")
input_amount.place(relx=0.65, rely=0.8, anchor="center", height=30)
root.mainloop()
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.
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)