How do I get image under the text In Tkinter? - python

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()

Related

How to add a scrollbar to a Text in tkinter?

I have made a code in which you type a question and it fetches an answer from Wikipedia once you press the 'Search' button. However decent I may be at python and tkinter, I'm stuck on this. I know how to add a scrollbar, set it, set it's command, set the y/xscrollcommand and pack it but it's not working and I could use some help.
Thanks in advance for any help I get.
#-----Import library-----#
import wikipedia
from tkinter import *
#-----Create window-----#
window=Tk()
iconpic=PhotoImage(file='Cat.png')
window.iconphoto(False, iconpic)
window.title('ChatCat')
window.geometry('400x517')
window.config(bg='#242121')
window.resizable(width=FALSE, height=FALSE)
#-----Define functions-----#
def two_sentence():
global number_o_sentences
number_o_sentences=2
def three_sentence():
global number_o_sentences
number_o_sentences=3
def four_sentence():
global number_o_sentences
number_o_sentences=4
def five_sentence():
global number_o_sentences
number_o_sentences=5
def reply():
Label(chatWindow, text=(messageWindow.get('1.0', 'end-1c')), wraplength='250', justify='right', fg='#502ec9', bg='#242121').pack(side='top', anchor='e', pady=3)
Label(chatWindow, text=(wikipedia.summary(messageWindow.get('1.0', 'end-1c'), sentences=number_o_sentences)), wraplength='350', justify='left', fg='#b02832', bg='#242121').pack(side='top', anchor='w', pady=3)
#-----Create menu-----#
main_menu=Menu(window)
main_menu.add_command(label="Two", command=two_sentence)
main_menu.add_command(label="Three", command=three_sentence)
main_menu.add_command(label="Four", command=four_sentence)
main_menu.add_command(label="Five", command=five_sentence)
main_menu.add_command(label="Quit", command=window.quit)
window.config(menu=main_menu)
#-----Create conversation window-----#
scrollbar = Scrollbar(window, cursor="target")
scrollbar.pack(side=RIGHT, fill='y')
chatWindow = Text(window, bd=1, bg="black", width="50", height="8", font=("Arial", 23), foreground="#00ffff", yscrollcommand=scrollbar.set)
chatWindow.place(x=6,y=6, height=385, width=370)
messageWindow = Text(window, bd=0, bg="black",width="30", height="4", font=("Arial", 23), foreground="#00ffff", highlightcolor='white', borderwidth=2)
messageWindow.place(x=128, y=400, height=88, width=247)
scrollbar.config(command=chatWindow.yview)
button=Button(window, text="Search", width="12", height=5, bd=0, bg="#2d3652", activebackground="#00bfff",foreground='#ffffff',font=("Arial", 12), command=reply)
button.place(x=6, y=400, height=88)
Label(chatWindow, text='Select the number of sentences you want above and start searching', wraplength='350', justify='left', fg='#b02832', bg='#242121').pack(side='top', anchor='w', pady=3)
#-----Run window-----#
window.mainloop()

How do I use grid_forget more than once in tkinter?

I'm trying to give myself Excercises using imports and keeping programs and modules separate from each other. In this particular excercise I've given myself, I have a basic feet-to-inches and inches-to-feet converter.
Everything works except for one problem. Where the answer is displayed on the GUI, a new answer covers the old answer, rather than replace.
import tkinter as tk
from tkinter import E, W
import Converter
window = tk.Tk()
window.title("Converter")
answer_label = tk.Label(window, text="No Answer")
answer_label.grid(row=5)
def feet_to_inches_function(old_label):
old_label.grid_forget()
answer_label = tk.Label(window, text=Converter.feet_to_inches(int(entry_bar.get())))
answer_label.grid(row=5)
def inches_to_feet_function(old_label):
old_label.grid_forget()
answer = tk.Label(window, text=Converter.inches_to_feet(int(entry_bar.get())))
answer.grid(row=5)
title_label = tk.Label(window, text="Convert")
entry_bar = tk.Entry(window, font=('HELVETICA', 10))
fti_button = tk.Button(window, text="Feet to Inches", command=lambda: feet_to_inches_function(answer_label))
itf_button = tk.Button(window, text="Inches to Feet", command=lambda: inches_to_feet_function(answer_label))
quit_button = tk.Button(window, text="Quit", command=window.destroy)
title_label.grid(row=0, column=0, sticky=W)
entry_bar.grid(row=1, columnspan=2, sticky=(E, W))
fti_button.grid(row=3, column=0)
itf_button.grid(row=3, column=1)
quit_button.grid(row=4, columnspan=2, sticky=(E, W))
window.mainloop()
Also as a separate question,I'm having a hard time understanding mainloop.I know that it makes the program go in an infinate loop, but from where? I don't see any logical evidence that shows that my code is on an endless loop.
Thanks so much
Here's a solution for you:
import tkinter as tk
from tkinter import E, W
window = tk.Tk()
window.title("Converter")
answer_label = tk.Label(window, text="No Answer")
answer_label.grid(row=5)
def feet_to_inches_function():
answer_label.configure(text='As you want (INCHES)')
def inches_to_feet_function():
answer_label.configure(text='As you want (FEET)')
title_label = tk.Label(window, text="Convert")
entry_bar = tk.Entry(window, font=('HELVETICA', 10))
fti_button = tk.Button(window, text="Feet to Inches", command=feet_to_inches_function)
itf_button = tk.Button(window, text="Inches to Feet", command=inches_to_feet_function)
quit_button = tk.Button(window, text="Quit", command=window.destroy)
title_label.grid(row=0, column=0, sticky=W)
entry_bar.grid(row=1, columnspan=2, sticky=(E, W))
fti_button.grid(row=3, column=0)
itf_button.grid(row=3, column=1)
quit_button.grid(row=4, columnspan=2, sticky=(E, W))
window.mainloop()
All you need is to configure the text of the Label in both functions.

Python (TKinter) GUI wont display text

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()

Changing part of a message's color in tkinter messagebox

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)

How to arrange buttons in a row?

I have defined separate functions, and have a main() function.
In the main() function, I have several buttons, which call to other functions when clicked.
eg.
def main():
global root
global tkinter
global canvas
root = Tk()
root.title("Shapes Quiz - Flow Computing")
Button(root, text ="1", bg="Grey", width=10, command=pentagon).pack()
Button(root, text ="2", bg="Grey", width=10, command=circle).pack()
Button(root, text ="3", bg="Grey", width=10, command=diamond).pack()
Button(root, text ="4", bg="Grey", width=10, command=hexagon).pack()
Button(root, text ="5", bg="Grey", width=10, command=triangle).pack()
Button(root, text ="6", bg="Grey", width=10, command=square).pack()
Button(root, text ="7", bg="Grey", width=10, command=rectangle).pack()
Button(root, text ="8", bg="Grey", width=10, command=heptagon).pack()
Button(root, text ="9", bg="Grey", width=10, command=octagon).pack()
Button(root, text ="10", bg="Grey", width=10, command=oval).pack()
Button(root, text ="Help", bg="Grey", width=10, command=help).pack()
Button(root, text ="Clear", bg="Red", width=10, command=clearshapes).pack()
Button(root, text ="QUIT", bg="Red", width=10, command=quit).pack()
I'm sure there must be an easier way to organize these buttons. I have heard of the use of frames and grids, and even co-ordinates; but I am still unsure of how to do this.
I would like to organize the buttons so they are horizontal, not vertical as they are appearing currently.
Change pack() to pack(side='left').
Definitely, there is an easier way to do this with the grid geometry manager (the link is already in the comments):
# ...
root.title("Shapes Quiz - Flow Computing")
BUTTONS = [{"text":"1", "bg":"Grey", "width":10, "command":pentagon},
{"text":"2", "bg":"Grey", "width":10, "command":circle},
# Rest of the buttons
{"text":"QUIT", "bg":"Red", "width":10, "command":quit}]
for i, options in enumerate(BUTTONS):
Button(root, **options).grid(row=0, column=i)
Note that you always use almost the same code, so it may look cleaner if you use a for loop and declare the options of the widgets in advance.

Categories

Resources