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)
Related
I am trying to create a TicTacToe game in Tkinter and I am trying to put a label under the buttons of the board but when I am put the label under the buttons the buttons are moving to the sides.
how can I fix it?
this is my code:
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title('TicTacToe')
# X starts
clicked = True
count = 0
#button clicked function
def b_click(b):
global clicked
global count
if b["text"] == " " and clicked == True:
b["text"] = "X"
clicked = False
count += 1
elif b["text"] == " " and clicked == False:
b["text"] = "O"
clicked = True
count += 1
else:
messages["text"] = "you cant do that"
#build the buttons
b1 = Button(root, text=" ", font =("Helvetica", 20), height=3, width=6, bg="SystemButtonFace", command= lambda:b_click(b1))
b2 = Button(root, text=" ", font =("Helvetica", 20), height=3, width=6, bg="SystemButtonFace", command= lambda:b_click(b2))
b3 = Button(root, text=" ", font =("Helvetica", 20), height=3, width=6, bg="SystemButtonFace", command= lambda:b_click(b3))
b4 = Button(root, text=" ", font =("Helvetica", 20), height=3, width=6, bg="SystemButtonFace", command= lambda:b_click(b4))
b5 = Button(root, text=" ", font =("Helvetica", 20), height=3, width=6, bg="SystemButtonFace", command= lambda:b_click(b5))
b6 = Button(root, text=" ", font =("Helvetica", 20), height=3, width=6, bg="SystemButtonFace", command= lambda:b_click(b6))
b7 = Button(root, text=" ", font =("Helvetica", 20), height=3, width=6, bg="SystemButtonFace", command= lambda:b_click(b7))
b8 = Button(root, text=" ", font =("Helvetica", 20), height=3, width=6, bg="SystemButtonFace", command= lambda:b_click(b8))
b9 = Button(root, text=" ", font =("Helvetica", 20), height=3, width=6, bg="SystemButtonFace", command= lambda:b_click(b9))
messages = Label(root, text="", font =("Helvetica", 20), height=1, width=15, relief="sunken")
space = Label(root, text="", font =("Helvetica",))
#display the buttons
b1.grid(row=0, column=0)
b2.grid(row=0, column=1)
b3.grid(row=0, column=2)
b4.grid(row=1, column=0)
b5.grid(row=1, column=1)
b6.grid(row=1, column=2)
b7.grid(row=2, column=0)
b8.grid(row=2, column=1)
b9.grid(row=2, column=2)
space.grid(row=3, column=0)
messages.grid(row=4, column=1)
root.mainloop()
When you try to add the label under the buttons, if you don't specify the columnspan the label will take the whole cell, so what happened is that the label is wider than the buttons, and since it took all the cell the buttons went to the sides, so if you put it like this:
...
space.grid(row=3, column=0)
messages.grid(row=4, column=0, columnspan=3)
root.mainloop()
you are telling tkinter that your label is taking the three columns, and so it does not move the buttons anymore. Here's the result:
I'm new to python and even newer to Tkinter. I'm trying to have the user enter the filename in an entry widget and hit a button that outputs the contents of the entered file to the console. The problem occurs in the attributeFileName() method (line 7) after button_1 (line 46) is pressed. Here is my code below:
from Tkinter import *
import tkMessageBox
root = Tk()
root.configure(background="black")
def attributeFileName():
try:
f = open(entry_1.get(), 'r') # Tries to open the file entered by the
except IOError:
print ("Cannot open", entry_1.get()) # If the file cannot be opened,
report it back to the user
for line in f:
print line
f.close()
def constraintFileName():
print(entry_2.get())
def preferenceFileName():
print(entry_3.get())
# *****Frames*****
fileFrame = Frame(root)
fileFrame.configure(background="black")
fileFrame.pack(pady=50)
attributeFrame = Frame(root)
attributeFrame.configure(background="red")
attributeFrame.pack(pady=5)
constraintFrame = Frame(root)
constraintFrame.configure(background="blue")
constraintFrame.pack(pady=5)
preferenceFrame = Frame(root)
preferenceFrame.configure(background="#51006F")
preferenceFrame.pack()
# *****File Frame*****
label_1 = Label(fileFrame, text="Enter Attributes file name:", anchor="e",
bg="red", font="Times 25", width=25, height=1)
label_2 = Label(fileFrame, text="Enter hard constraints file name:",
anchor="e", bg="blue", fg="yellow", font="Times 25", width=25, height=1)
label_3 = Label(fileFrame, text="Enter preferences file name:",
anchor="e", bg="#51006F", fg="#45FFF4", font="times 25", width=25,
height=1)
entry_1 = Entry(fileFrame, font="Times 25")
entry_2 = Entry(fileFrame, font="Times 25")
entry_3 = Entry(fileFrame, font="Times 25")
button_1 = Button(fileFrame, text="Submit", bg="red", font="Times 20",
command=attributeFileName)
button_2 = Button(fileFrame, text="Submit", bg="blue", fg="yellow",
font="Times 20", command=constraintFileName)
button_3 = Button(fileFrame, text="Submit", bg="#51006F", fg="#45FFF4",
font="Times 20", command=preferenceFileName)
label_1.grid(row=0, column=0, padx=5, pady=5)
entry_1.grid(row=0, column=1)
button_1.grid(row=0, column=2, padx=5, pady=5)
label_2.grid(row=1, column=0, padx=5, pady=5)
entry_2.grid(row=1, column=1)
button_2.grid(row=1, column=2, padx=5, pady=5)
label_3.grid(row=2, column=0, padx=5, pady=5)
entry_3.grid(row=2, column=1)
button_3.grid(row=2, column=2, padx=5, pady=5)
# *****Attribute Frame*****
attributeHeaderFrame = Frame(attributeFrame)
attributeHeaderFrame.configure(background="red")
attributeHeaderFrame.grid(row=0)
attributeDataFrame = Frame(attributeFrame)
attributeDataFrame.configure(background="red")
attributeDataFrame.grid(row=1)
attributeListFrame = Frame(attributeFrame)
attributeListFrame.configure(background="red")
attributeListFrame.grid(row=2, pady=10)
label_Attribute_header = Label(attributeHeaderFrame, text="Attributes",
bg="red", font="Times 25", width=25, height=1)
attribute_Name = Label(attributeDataFrame, text="Name:", bg="red",
font="Times 20")
entry_Attribute_Name = Entry(attributeDataFrame, font="Times 20")
label_Colon = Label(attributeDataFrame, text=":", bg="red", font="Times
20")
label_Comma = Label(attributeDataFrame, text=",", bg="red", font="Times
25")
entry_Attribute1 = Entry(attributeDataFrame, font="Times 20")
entry_Attribute2 = Entry(attributeDataFrame, font="Times 20")
attribute_add = Button(attributeDataFrame, text="+", bg="black",
fg="white", font="Times 15")
list = Listbox(attributeListFrame, height=15, width=172)
scroll = Scrollbar(attributeListFrame, command=list.yview)
list.configure(yscrollcommand = scroll.set)
list.pack(side=LEFT)
scroll.pack(side=RIGHT, fill=Y)
label_Attribute_header.pack()
attribute_Name.grid(row=0, column=0, padx=5, pady=5, sticky="W")
entry_Attribute_Name.grid(row=0, column=1, sticky="W")
label_Colon.grid(row=0, column=2, sticky="W")
entry_Attribute1.grid(row=0, column=3, sticky="W", padx=(50,0))
label_Comma.grid(row=0, column=4, sticky="W")
entry_Attribute2.grid(row=0, column=5, sticky="W")
attribute_add.grid(row=0, column=6, padx=5, pady=5, sticky="W")
# *****Constraint Frame*****
constraintHeaderFrame = Frame(constraintFrame)
constraintHeaderFrame.configure(background="blue")
constraintHeaderFrame.grid(row=0)
constraintDataFrame = Frame(constraintFrame)
constraintDataFrame.configure(background="blue")
constraintDataFrame.grid(row=1, pady=10)
constraintListFrame = Frame(constraintFrame)
constraintListFrame.configure(background="blue")
constraintListFrame.grid(row=2, pady=10)
label_Constraint_header = Label(constraintHeaderFrame, text="Hard
Constraints", bg="blue", fg="yellow", font="Times 25")
label_Constraints = Label(constraintDataFrame, text="Constraint:",
bg="blue", fg="yellow", font="Times 20",)
entry_Constraints = Entry(constraintDataFrame, font="Times 20")
constraint_add = Button(constraintDataFrame, text="+", bg="black",
fg="white", font="Times 15")
label_Constraint_header.pack()
label_Constraints.grid(row=0)
entry_Constraints.grid(row=0, column=1)
constraint_add.grid(row=0, column=2, padx=15)
list = Listbox(constraintListFrame, height=15, width=172)
scroll = Scrollbar(constraintListFrame, command=list.yview)
list.configure(yscrollcommand = scroll.set)
list.pack(side=LEFT)
scroll.pack(side=RIGHT, fill=Y)
# *****Preference Frame*****
preferenceHeaderFrame = Frame(preferenceFrame)
preferenceHeaderFrame.configure(background="#51006F")
preferenceHeaderFrame.grid(row=0)
preferenceDataFrame = Frame(preferenceFrame)
preferenceDataFrame.configure(background="#51006F")
preferenceDataFrame.grid(row=1, pady=10)
preferenceListFrame = Frame(preferenceFrame)
preferenceListFrame.configure(background="#51006F")
preferenceListFrame.grid(row=2, pady=10)
label_Preference_header = Label(preferenceHeaderFrame, text="Preferences",
bg="#51006F", fg="#45FFF4", font="Times 25")
label_preference = Label(preferenceDataFrame, text="Preference:",
bg="#51006F", fg="#45FFF4", font="Times 20",)
entry_preference = Entry(preferenceDataFrame, font="Times 20")
preference_add = Button(preferenceDataFrame, text="+", bg="black",
fg="white", font="Times 15")
label_Preference_header.pack()
label_preference.grid(row=0)
entry_preference.grid(row=0, column=1)
preference_add.grid(row=0, column=2, padx=15)
list = Listbox(preferenceListFrame, height=15, width=172)
scroll = Scrollbar(preferenceListFrame, command=list.yview)
list.configure(yscrollcommand = scroll.set)
list.pack(side=LEFT)
scroll.pack(side=RIGHT, fill=Y)
root.mainloop()
My Output:
I expect the name of the file to be read from the entry widget and the file contents printed out to the console. However, instead an Exception is thrown. Here is the stack trace:
('Cannot open', 'test.txt')
Exception in Tkinter callback
Traceback (most recent call last):
File "Tkinter.py", line 1542, in __call__
return self.func(*args)
File "gui.py", line 13, in attributeFileName
for line in f:
UnboundLocalError: local variable 'f' referenced before assignment
Can somebody explain to me what this means, why I'm getting this error, and most importantly how to fix it? Thanks
In this function:
def attributeFileName():
try:
f = open(entry_1.get(), 'r') # Tries to open the file entered by the
except IOError:
print ("Cannot open", entry_1.get()) # If the file cannot be opened, report it back to the user
for line in f:
print (line)
f.close()
If an exception is caught, f is undefined and result in your unbound error. Simply move the for loop and close inside the try statement:
def attributeFileName():
try:
f = open(entry_1.get(), 'r')
for line in f:
print(line)
f.close()
except IOError:
print ("Cannot open", entry_1.get())
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 found some code to create a menu bar in TkInter, but I can't put in my code without it bugging.
Here is the code I'm using:
from Tkinter import *
import subprocess
import os
master = Tk()
master.geometry("1000x668")
master.title("Menu")
master.configure(background='pale green')
master.iconbitmap(r"C:\Users\André\Desktop\python\menu.ico")
master = Tk()
def NewFile():
print "New File!"
def OpenFile():
name = askopenfilename()
print name
def About():
print "This is a simple example of a menu"
menu = Menu(master)
master.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=NewFile)
filemenu.add_command(label="Open...", command=OpenFile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=master.quit)
w = Label(master, text="Abrir", bg="pale green", fg="steel blue", font=("Algerian", 20, "bold"))
w.pack()
w.place(x=100, y=0)
def notepad():
subprocess.Popen("notepad.exe")
buttonote = Button(master, text="Bloco de notas", wraplength=50, justify=CENTER, padx=2, bg="light sea green", height=2, width=7, command=notepad)
buttonote.pack()
buttonote.place(x=0, y=50)
def regedit():
subprocess.Popen("regedit.exe")
buttonreg = Button(master, text="Editor de Registo", wraplength=50, justify=CENTER, padx=2, bg="light sea green", height=2, width=7, command=regedit)
buttonreg.pack()
buttonreg.place(x=60, y=50)
def skype():
subprocess.Popen("skype.exe")
buttonskype = Button(master, text="Skype", bg="light sea green", height=2, width=7, command=skype)
buttonskype.pack()
buttonskype.place(x=120, y=50)
def steam():
os.startfile("D:\Steam\Steam.exe")
buttonsteam = Button(master, text="Steam", bg="light sea green", height=2, width=7, command=steam)
buttonsteam.pack()
buttonsteam.place(x=178, y=50)
e1 = Entry(master, width=15)
e1.pack(padx=100,pady=4, ipadx=2)
def save():
text = e1.get()
SaveFile = open('information.txt','w')
SaveFile.write(text)
SaveFile.close()
nome = Label(master, text="Nome?", bg="pale green", fg="steel blue", font=("Arial Black", 12))
nome.pack()
nome.place(x=380, y=0)
buttonsave = Button(master, text="Guardar", bg="light sea green", height=1, width=6, command=save)
buttonsave.pack()
buttonsave.place(x=550, y=0)
f = open('information.txt','r')
line = f.readline()
show = Label(master, text=line, bg="pale green", fg="steel blue", font=("Arial Black", 12))
show.pack()
show.place(x=32, y=640)
hi = Label(master, text='Hi, ', bg="pale green", fg="steel blue", font=("Arial Black", 12))
hi.pack()
hi.place(x=0, y=640)
master.mainloop()
Can anyone work out what's wrong with my code? Thanks!
You have master = Tk() on line 4 and repeated on line 9. Delete line 9.
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)