The code:
from tkinter import*
root = Tk()
root.title("Mood Control")
root.geometry("500x500")
label1 = Label(root, text="From 1-5 how would you rate your mood today? ", font=("arial",15, "bold"), fg="black").place(x=10, y=40)
def sad():
photo = PhotoImage(file="happyturtle.gif")
label = Label(image=photo).place(x=135, y=200)
label.image = photo
label.pack()
def happy():
photo = PhotoImage(file="sadcat.gif")
label = Label(image=photo).place(x=100, y=250)
label.image = photo
label.pack()
label2 = labe
button1 = Button(root, text="1", width=3, height=3, bg="lightgrey", command=sad).place(x=20, y=100)
button2 = Button(root, text="2", width=3, height=3, bg="white", command=sad).place(x=60, y=100)
button3 = Button(root, text="3", width=3, height=3, bg="lightgrey", command=happy).place(x=100, y=100)
button4 = Button(root, text="4", width=3, height=3, bg="white", command=happy).place(x=140, y=100)
button5 = Button(root, text="5", width=3, height=3, bg="lightgrey", command=happy).place(x=180, y=100)
root.mainloop()
Essentially im trying to make a fun little app that controls your mood, you can never be too happy or sad. So if I were to press 1 or 2 it would display a pictyre of a happy turtle to cheer you up.
if I were to press 3-5 it would show a picture of a sad cat to make sure you're not too happy. I have everything figured out but I have one problem, I would like to have text above the images once they are displayed, for example "You are
too happy, here's a pciture of a sad cat". But when I attempt to do this it never displays the text. I have attempted to do this like this:
def happy():
photo = PhotoImage(file="sadcat.gif")
label = Label(image=photo).place(x=100, y=250)
label.image = photo
label.pack()
label2 = Label(root, text="You are too happy, here's a picture of a sad cat", font=("arial",10, "bold"), fg="black").place(x=100, y=40)
# above line is what is supposed to display the text, it doesnt.
Also of I mouse over "label2" it says "local variable "label2" is not used". How do I fix this?
Any answers are appriciated
There are a few issues with the happy function.
def happy():
photo = PhotoImage(file="sadcat.gif")
label = Label(image=photo).place(x=100, y=250)
label.image = photo
label.pack()
label2 = Label(root, text="You are too happy, here's a picture of a sad cat", font=("arial",10, "bold"), fg="black").place(x=100, y=40)
# above line is what is supposed to display the text, it doesnt.
The line label = Label(image=photo).place(x=100, y=250) sets the label variable to the return from place(). The object returned by place will not have an image attribute so the line label.image = photo, will cause error. If you want a reference to the label try:
photo = PhotoImage(file="sadcat.gif")
label = Label(image=photo)
label.place(x=100, y=250)
Now label.image = photo will not cause error, but this line can be taken out as you set the image when you do Label(image=photo). label.pack() should be removed as you are using the place geometry manager already.
Leaving:
def happy():
photo = PhotoImage(file="sadcat.gif")
label = Label(image=photo).place(x=100, y=250)
label2 = Label(root, text="You are too happy, here's a picture of a sad cat", font=("arial",10, "bold"), fg="black").place(x=100, y=40)
# above line is what is supposed to display the text, it doesnt.
Edit:
from tkinter import*
root = Tk()
root.title("Mood Control")
root.geometry("500x500")
label1 = Label(root, text="From 1-5 how would you rate your mood today? ", font=("arial",15, "bold"), fg="black")
label1.place(x=10, y=40)
def sad():
photo = PhotoImage(file="tenor1.gif")
label = Label(root, image=photo)
label.place(x=100, y=250)
label.image = photo
def happy():
label1['text'] = "You are too happy, here's a picture of a sad cat"
photo = PhotoImage(file="tenor.gif")
label = Label(root, image=photo)
label.place(x=100, y=250)
label.image = photo
# above line is what is supposed to display the text, it doesnt.
button1 = Button(root, text="1", width=3, height=3, bg="lightgrey", command=sad).place(x=20, y=100)
button2 = Button(root, text="2", width=3, height=3, bg="white", command=sad).place(x=60, y=100)
button3 = Button(root, text="3", width=3, height=3, bg="lightgrey", command=happy).place(x=100, y=100)
button4 = Button(root, text="4", width=3, height=3, bg="white", command=happy).place(x=140, y=100)
button5 = Button(root, text="5", width=3, height=3, bg="lightgrey", command=happy).place(x=180, y=100)
root.mainloop()
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 recently started learning Python and Tkinter. I started about a month ago. But let's get to the point.
I have been making a random software (so don't mock) for 1 day now, and the code is ready, but I still need to make the app itself, so I have been doing it, but face a problem when making background changing buttons. I made buttons which change the background to either a colour or a picture.
The normal colours work really well, but the image seems to be acting weird. I got the image appearing from the button, but the problem is, when I click the button, the image displays OVER the texts, entry etc.
"Code Fix" that works for a non-changing picture:
Tkinter.Label(window, i=bgimg, compound=Tkinter.CENTER).pack()
I haven't seen many people have this problem. I saw one, and that worked (if I would have a non-changeable background), but because I have the buttons, the code just doesn't care about it. (The code that would have helped for a non-changing picture.)
Here is the code:
from tkinter import *
import tkinter as tk
import tkinter
window=Tk()
bgimg= tk.PhotoImage(file = "C:\\Users\\Hoze\\Downloads\\Hoze (1).png")
btn=Button(window, text="Convert", fg='blue', padx=50)
btn.place(x=220, y=380)
def myClick():
myLabel = Label(window, window.configure(bg='red'))
myLabel.pack()
btn=Button(window, text="Red", fg='red', padx=20, command=myClick)
btn.place(x=520, y=150)
def myClick():
myLabel = Label(window, window.configure(bg='blue'))
myLabel.pack()
btn=Button(window, text="Blue", fg='blue', padx=19, command=myClick)
btn.place(x=520, y=200)
def myClick():
myLabel = Label(window, window.configure(bg='green'))
myLabel.pack()
btn=Button(window, text="Green", fg='green', padx=15, command=myClick)
btn.place(x=520, y=300)
def myClick():
myLabel = Label(window, window.configure(bg='orchid'))
myLabel.pack()
btn=Button(window, text="Default", fg='orchid', padx=11, command=myClick)
btn.place(x=520, y=250)
# Here is the problem, and here is the code that
# put the picture like it should UNDER the text
# (if i put the code to the start)
# (But then it does't change to the other colours)
def myClick():
myLabel= Label(window, i=bgimg, )
myLabel.pack()
btn=Button(window, text="Cool", fg='aqua', padx=11, command=myClick)
btn.place(x=520, y=250)
# If i now run this code, the picture will come
# on top of the text.
lbl=Label(window, text=" Extract a word here, \n that you want make secret!", fg='blue', font=("Helvetica", 16))
lbl.place(x=166 , y=50)
lbl=Label(window, text="Your Secret Word is: ", fg='blue', font=("Helvetica", 10))
lbl.place(x=120, y=300)
txtfld=Entry(window, text="This is Entry Widget", bd=5)
txtfld.place(x=234, y=150)
window.title('Hoze Secret Lenguage Converter')
window.geometry("600x500+10+10")
window.minsize(600, 500)
window.maxsize(600, 500)
window.iconbitmap("C:\\Users\\Hoze\\Music\\Coding\\Project Secret\\favicon.ico")
window.configure(bg='green')
window.mainloop()
This should make your stuff
#import tkinter as tk
from email.mime import image
from tkinter import *
window=Tk()
bgimg= PhotoImage(file = "C:\\Users\\Hoze\\Downloads\\Hoze (1).png")
myLabel= Label(window, i=bgimg )
myLabel.pack()
myLabel.pack_forget()
btn=Button(window, text="Convert", fg='blue', padx=50)
btn.place(x=220, y=380)
def myClick():
myLabel.pack_forget()
window.configure(bg='red')
btn=Button(window, text="Red", fg='red', padx=20, command=myClick)
btn.place(x=520, y=150)
def myClick():
myLabel.pack_forget()
window.configure(bg='blue')
btn=Button(window, text="Blue", fg='blue', padx=19, command=myClick)
btn.place(x=520, y=200)
def myClick():
myLabel.pack_forget()
window.configure(bg='green')
btn=Button(window, text="Green", fg='green', padx=15, command=myClick)
btn.place(x=520, y=300)
def myClick():
myLabel.pack_forget()
window.configure(bg='orchid')
btn=Button(window, text="Default", fg='orchid', padx=11, command=myClick)
btn.place(x=520, y=250)
#Here is the problem, and here is the code that put the picture like it should UNDER the text (if i put the code to the start)(But then it does't change to the other colours)
def myClick():
myLabel.pack()
btn=Button(window, text="Cool", fg='aqua', padx=11, command=myClick)
btn.place(x=520, y=250)
#If i now run this code, the picture will come on top of the text. I hope yall get it, im kinda new
#
lbl=Label(window, text=" Extract a word here, \n that you want make secret!", fg='blue', font=("Helvetica", 16))
lbl.place(x=166 , y=50)
lbl=Label(window, text="Your Secret Word is: ", fg='blue', font=("Helvetica", 10))
lbl.place(x=120, y=300)
txtfld=Entry(window, text="This is Entry Widget", bd=5)
txtfld.place(x=234, y=150)
window.title('Hoze Secret Lenguage Converter')
window.geometry("600x500+10+10")
window.minsize(600, 500)
window.maxsize(600, 500)
window.iconbitmap("C:\\Users\\Hoze\\Music\\Coding\\Project Secret\\favicon.ico")
window.configure(bg='green')
window.mainloop()
For my project, I need to create a GUI using Tkinter. I wanted to divide it into 4 different frames, each with different background colors. However, when I do this, only the first frame's background color is shown. I attached my code below as well as a screenshot of the output I'm getting.
from Tkinter import *
import tkMessageBox
root = Tk()
root.geometry("950x800")
root.configure(background="black")
def test():
print("this is a test")
# *****Frames*****
fileFrame = Frame(root)
fileFrame.configure(background="yellow")
fileFrame.pack()
attributeFrame = Frame(root)
attributeFrame.configure(background="red")
attributeFrame.pack()
constraintFrame = Frame(root)
constraintFrame.configure(background="purple")
constraintFrame.pack()
preferenceFrame = Frame(root)
preferenceFrame.configure(background="blue")
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="purple", fg="green", 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")
button_2 = Button(fileFrame, text="Submit", bg="blue", fg="yellow",
font="Times 20")
button_3 = Button(fileFrame, text="Submit", bg="purple", fg="green",
font="Times 20")
label_1.grid(row=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, 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, padx=5, pady=5)
entry_3.grid(row=2, column=1)
button_3.grid(row=2, column=2, padx=5, pady=5)
# *****Attribute Frame*****
label_Attribute_header = Label(attributeFrame, text="Attributes:",
bg="red", font="Times 25", width=25, height=1)
label_Attribute_header.pack()
# *****Constraint Frame*****
label_Constraint_header = Label(constraintFrame, text="Hard Constraints:",
bg="purple", fg="green", font="Times 25", width=25, height=1)
label_Constraint_header.pack()
# *****Preference Frame*****
label_Preference_header = Label(preferenceFrame, text="Preferences:",
bg="blue", fg="yellow", font="Times 25", width=25, height=1)
label_Preference_header.pack()
root.mainloop()
I expect there to be 4 different frames, all stacked on top of each other with different background colors, but instead only the first frame has a background color. Can somebody explain to me why this is and how I should go about fixing this? Thanks
Your frames are there. The bottom three frames have fewer things in them and you haven't given them any padding. The frame shrinks to fit, so when you just have one item, you won't see the frames.
You can easily see the frames if you do one of two things:
First, you can request that the frames fill their parent window in the x direction. When you do this, you'll see them:
fileFrame.pack(fill="x")
attributeFrame.pack(fill="x")
constraintFrame.pack(fill="x")
preferenceFrame.pack(fill="x")
Second, instead of or in addition to that, you can give padding around the labels in the bottom frames. That will let the frame colors appear.
label_Attribute_header.pack(padx=20, pady=20)
...
label_Constraint_header.pack(padx=20, pady=20)
...
label_Preference_header.pack(padx=20, pady=20)
I have a TKinter messagebox like the one below. I would like to change part of the message to a different color. For example in the messagebox below I would like the language to be Blue. Is this possible?
It's not possible to change such options of Tkinter Standard Dialogs. You need to create your own dialog. You'll also need to separate the text parts. I've tried to make something similar in the image that the OP has posted above:
from tkinter import *
root = Tk()
def choosefunc(option):
if option == "cancel":
print("Cancel choosen")
else:
print("OK choosen")
def popupfunc():
tl = Toplevel(root)
tl.title("Languages")
frame = Frame(tl)
frame.grid()
canvas = Canvas(frame, width=100, height=130)
canvas.grid(row=1, column=0)
imgvar = PhotoImage(file="pyrocket.png")
canvas.create_image(50,70, image=imgvar)
canvas.image = imgvar
msgbody1 = Label(frame, text="The", font=("Times New Roman", 20, "bold"))
msgbody1.grid(row=1, column=1, sticky=N)
lang = Label(frame, text="language(s)", font=("Times New Roman", 20, "bold"), fg='blue')
lang.grid(row=1, column=2, sticky=N)
msgbody2 = Label(frame, text="of this country is: Arabic", font=("Times New Roman", 20, "bold"))
msgbody2.grid(row=1, column=3, sticky=N)
cancelbttn = Button(frame, text="Cancel", command=lambda: choosefunc("cancel"), width=10)
cancelbttn.grid(row=2, column=3)
okbttn = Button(frame, text="OK", command=lambda: choosefunc("ok"), width=10)
okbttn.grid(row=2, column=4)
label = Label(root, text="Click to proceed:")
label.grid()
button = Button(root, text="Click", command=popupfunc)
button.grid()
(Image URL: http://imgur.com/a/Nf75v)
I have 2 functions, MakeGUI:
def makeGUI():
#Define the global variables of the function.
global root,f1,f2,f3,r,y,b,g,patColor
#Initialize Gui Items.
root = Tk()
root.wm_title("Buttons")
f3 = Frame(root)
f1 = Frame(root)
f2 = Frame(root)
r = Button(f1, text= "Red", fg="Black", bg="Red", width=25, font="TimesNewRoman", bd=1)
y = Button(f1, text= "Yellow", fg="Black", bg="Yellow", width=25, font="TimesNewRoman", bd=1)
b = Button(f2, text= "Blue", fg="Black", bg="Blue", width=25, font="TimesNewRoman", bd=1, command=showPattern)
g = Button(f2, text= "Green", fg="Black", bg="Green", width=25, font="TimesNewRoman", bd=1)
patColor = Label(f3, bg="White", width=66)
#Pack the GUI items so they will show.
f3.pack()
f1.pack()
f2.pack()
r.pack(side=LEFT)
y.pack(side=RIGHT)
b.pack(side=LEFT)
g.pack(side=RIGHT)
patColor.pack()
#Show the GUI.
root.mainloop()
And showPattern:
def showPattern():
patColor.bg = "Blue"
Is there any way to update only the patColor bg property without refreshing the whole GUI? I am making a simon says type game with python 2.7 and i need it to cycle through a pattern array.
I've discovered after looking into more that the proper notation is:
patColor["bg"] = "Color"