Related
I am a novice and working with Tkinter for the first time. My project is an "I Spy" book which moves picture to picture utilizing buttons. I like the look of the LabelFrame widget and want to use the title for the text that corresponds with each image. I am able to achieve the images updating correctly but the title remains the same. I have tried config, forgetting and then rebuilding the frame, and I think something else which I can't recall and none of that has worked. I have searched online, reviewed Stack Overflow similar questions--which are very few and lead me to believe that this cannot be done. Thank you for your assistance.
from tkinter import *
from tkinter import Button
from tkinter import Label
from PIL import ImageTk
from PIL import Image
root = Tk()
root.title('')
root.attributes('-toolwindow', True)
root.geometry('620x660+100+0')
img2 = ImageTk.PhotoImage(Image.open('spy_images/rainbow.jpg'))
img4 = ImageTk.PhotoImage(Image.open('spy_images/pods.jpg'))
img5 = ImageTk.PhotoImage(Image.open('spy_images/lion.jpg'))
img6 = ImageTk.PhotoImage(Image.open('spy_images/bike.jpg'))
img7 = ImageTk.PhotoImage(Image.open('spy_images/binary.jpg'))
image_list = [img2, img4, img5, img6, img7]
text2 = 'A rainbow, not in the sky!'
text4 = 'Dangly, weird seed pods.'
text5 = 'A stoney grin.'
text6 = 'A lane just for bikes.'
text7 = 'A different way to count.'
text_list = [text2, text4, text5, text6, text7]
make_frame = LabelFrame(root, text='A rainbow, not in the sky!', width=100, height=100,
font=('Arial', 14, 'bold'), fg='red', bd=10)
make_frame.grid(row=0, column=1, columnspan=5)
img_filename = 'spy_images/rainbow.jpg'
PIL_image = Image.open(img_filename)
img = ImageTk.PhotoImage(PIL_image)
in_frame = Label(make_frame, image=img)
in_frame.grid(padx=10, pady=10)
def forward(image_num, text_num):
global make_frame
global in_frame
global button_forward
global button_back
in_frame.grid_forget()
in_frame = Label(image=image_list[image_num])
button_forward = Button(root, text='>>', command=lambda:
forward(image_num+1, text_num+1))
button_back = Button(root, text='<<', command=lambda:
back(image_num-1, text_num-1))
if image_num == 7:
button_forward = Button(root, text='>>', state=DISABLED)
make_frame.grid(row=0, column=1, columnspan=5)
in_frame.grid(row=0, column=0, columnspan=5)
in_frame.grid(padx=10, pady=10)
button_forward.grid(row=1, column=5)
button_back.grid(row=1, column=1)
button_back.grid_columnconfigure(0, weight=1)
button_back.grid_columnconfigure(2, weight=1)
button_back.grid_columnconfigure(4, weight=1)
def back(image_num, text_num):
global make_frame
global in_frame
global button_forward
global button_back
in_frame.grid_forget()
in_frame = Label(image=image_list[image_num - 1])
button_forward = Button(root, text='>>', command=lambda:
forward(image_num + 1, text_num + 1))
button_back = Button(root, text='<<', command=lambda:
back(image_num - 1, text_num - 1))
if image_num == 1:
button_back = Button(root, text='<<', state=DISABLED)
make_frame.grid(row=0, column=1, columnspan=5)
in_frame.grid(row=0, column=0, columnspan=3)
in_frame.grid(padx=10, pady=10)
button_forward.grid(row=1, column=5)
button_back.grid(row=1, column=1)
button_back.grid_columnconfigure(0, weight=1)
button_back.grid_columnconfigure(2, weight=1)
button_back.grid_columnconfigure(4, weight=1)
button_back = Button(root, text='<<', command=back, state=DISABLED, bg='#d9d5d4',
font=('Arial', 14, 'bold'))
button_exit = Button(root, text='Cancel', command=root.quit, bg='#d9d5d4', font=('Arial', 12))
button_forward = Button(root, text='>>', command=lambda: forward(2, 2), bg='#d9d5d4', font=('Arial', 14, 'bold'))
button_back.grid(row=1, column=1)
button_exit.grid(row=1, column=3)
button_forward.grid(row=1, column=5)
button_back.grid_columnconfigure(0, weight=1)
button_back.grid_columnconfigure(2, weight=1)
button_back.grid_columnconfigure(4, weight=1)
root.mainloop()
If you have some questions about the exampel I'll try to answer you.
import tkinter as tk
#you dont need this but if you want to cycle, wich is would be nice there you go
from itertools import cycle
#create a list with strings you like to display
li = ['A rainbow, not in the sky!','Dangly, weird seed pods.','A stoney grin.',
'A lane just for bikes.','A different way to count.']
#here we create a cycle of that list
my_cycled_li = cycle(li)
#the change function
def change():
#set var to next element in list
var.set(next(my_cycled_li))
#update the LabelFrame
lf.configure(text=var.get())
root = tk.Tk()
#variable to change
var = tk.StringVar()
#there can be a default setting
var.set('default')
lf = tk.LabelFrame(root,text=var.get(),width=200,height=100,bg='red')
#you dont need this, this means the Frame size isnt the size of the widget it contains.
lf.pack_propagate(0)
lf.pack()
b = tk.Button(lf,text='change', command=change)
b.pack()
root.mainloop()
hoped it helps.
I have a window with two parts. One part is to do some settings. I want to hide it until the user press a setting button. is it possible to hide a part of the frame that contains many widgets?
I have seen many examples to hide a widget in tkinter (eg. pack_forget and grid_forget). In my case, I am trying to hide a part of the frame through a button press (that contains more than one widgets). Any suggestions please
I can't use more than one frames because of some issues.
import tkinter as tk
def startFn():
pass
#fn body
def stopFn():
pass
#fn body
def FnToShow():
pass
#fn body ???
def FnToHide():
pass
#fn body ???
root = tk.Tk()
root.geometry('600x400')
#two containers like this.
#trying to hide container_2 untill the user press settingBtn
container_1 = tk.Frame(root, borderwidth=2, relief="solid")
container_2 = tk.Frame(root, borderwidth=2, relief="solid")
startBtn = tk.Button(container_1, text = "Start", command =startFn)
startBtn.grid(row=4, column=4)
stopBtn = tk.Button(container_1, text = "Stop", command= stopFn)
stopBtn.grid(row=5, column=4)
settingBtn = tk.Button(container_1, text = "Settings", command= FnToShow)
settingBtn.grid(row=6, column=4)
setting_1 = tk.Label(container_2, text = "Setting-1", fg='#000000')
setting_1.grid(row=3, column=10)
setting_2 = tk.Label(container_2, text = "Setting-2", fg='#000000')
setting_2.grid(row=4, column=10)
closeSettingBtn = tk.Button(container_2, text = "close Settings", command= FnToHide)
closeSettingBtn.grid(row=5, column=10)
container_1.pack(side="left", expand=True, fill="x", padx=1, pady=1)
container_2.pack(side="right",expand=True, fill="x", padx=1, pady=1)
root.mainloop()
You could show/hide the entire container_2 using the functions FnToShow and FnToHide:
something like this:
import tkinter as tk
def startFn():
pass
def stopFn():
pass
def FnToShow():
container_2.pack(side="right",expand=True, fill="x", padx=1, pady=1)
def FnToHide():
container_2.pack_forget()
root = tk.Tk()
root.geometry('600x400')
container_1 = tk.Frame(root, borderwidth=2, relief="solid")
container_2 = tk.Frame(root, borderwidth=2, relief="solid")
startBtn = tk.Button(container_1, text="Start", command =startFn)
startBtn.grid(row=4, column=4)
stopBtn = tk.Button(container_1, text="Stop", command= stopFn)
stopBtn.grid(row=5, column=4)
settingBtn = tk.Button(container_1, text="Settings", command= FnToShow)
settingBtn.grid(row=6, column=4)
setting_1 = tk.Label(container_2, text="Setting-1", fg='#000000')
setting_1.grid(row=3, column=10)
setting_2 = tk.Label(container_2, text="Setting-2", fg='#000000')
setting_2.grid(row=4, column=10)
closeSettingBtn = tk.Button(container_2, text="close Settings", command= FnToHide)
closeSettingBtn.grid(row=5, column=10)
container_1.pack(side="left", expand=True, fill="x", padx=1, pady=1)
root.mainloop()
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()
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 am in the process of learning basic Python. I am currently attempting to create a simple calculator program that only has addition and subtraction. I have one issue though. I am not sure how I would add text to my Python label upon button press. Right now, upon pressing the '1' button, my program will change the display label to the text "1". However, I want my program to add text, not set it.
For example, if I press 'button 1' 5 times, it currently will reset the label text 5 times and will result with a single 1. I want it to add the number to the label upon press, not replace.
Current Result after pressing button 5 times: 1
Requested result after pressing button 5 times: 11111
Here is my current code for the program. If anything is unclear, just ask; thanks.
from tkinter import *
window = Tk()
# Creating main label
display = Label(window, text="")
display.grid(row=0, columnspan=3)
def add_one():
display.config(text='1')
# Creating all number buttons
one = Button(window, text="1", height=10, width=10, command=add_one)
two = Button(window, text="2", height=10, width=10)
three = Button(window, text="3", height=10, width=10)
four = Button(window, text="4", height=10, width=10)
five = Button(window, text="5", height=10, width=10)
six = Button(window, text="6", height=10, width=10)
seven = Button(window, text="7", height=10, width=10)
eight = Button(window, text="8", height=10, width=10)
nine = Button(window, text="9", height=10, width=10)
zero = Button(window, text="0", height=10, width=10)
# Placing all number buttons
one.grid(row=1, column=0)
two.grid(row=1, column=1)
three.grid(row=1, column=2)
four.grid(row=2, column=0)
five.grid(row=2, column=1)
six.grid(row=2, column=2)
seven.grid(row=3, column=0)
eight.grid(row=3, column=1)
nine.grid(row=3, column=2)
# Creating all other buttons
add = Button(window, text="+", height=10, width=10)
subtract = Button(window, text="-", height=10, width=10)
equal = Button(window, text="=", height=10, width=10)
# Placing all other buttons
add.grid(row=4, column=0)
subtract.grid(row=4, column=1)
equal.grid(row=4, column=2)
window.mainloop()
You should use a StringVar for this. And your callback needs to get the current contents of the StringVar, modify it, and use the modified string to set the new value of the StringVar. Like this:
import tkinter as tk
window = tk.Tk()
# Creating main label
display_text = tk.StringVar()
display = tk.Label(window, textvariable=display_text)
display.grid(row=0, columnspan=3)
def add_one():
s = display_text.get()
s += '1'
display_text.set(s)
one = tk.Button(window, text="1", height=10, width=10, command=add_one)
one.grid(row=1, column=0)
window.mainloop()
BTW, you should try to make your program a little more compact by using for loops to create and lay out your buttons, in accordance with the DRY principle.
Also, it's not a good idea to use from tkinter import *. It imports over 130 names into your namespace, making it easy to create name collisions if you accidentally use a Tkinter name for one of your own variables or functions.
You can define add_one like the following, to first get the existing value and then append a new value to it:
def add_one():
current_value = display.cget("text")
new_value = current_value + "1"
display.config(text=new_value)
Is this what you're looking for:
from tkinter import *
root = Tk()
var = StringVar()
def f1():
var.set(" ")
var.set("1")
def f2():
var.set(" ")
var.set("2")
label = Label(root, textvariable=var)
label.pack()
button1 = Button(root, text="One", command=f1)
button1.pack()
button2 = Button(root, text="Two", command=f2)
button2.pack()
?