I'm quite new to coding and I'm trying to have an image show up in a tkinter window.
However, when I run the code below there is the gap where the image should be. I am getting no error from this code as well.
window2 = Toplevel()
window2.geometry("1920x1200")
Namearea = Label(window2, text="Please Name the Prebuild:")
Namearea.pack()
e = Entry(window2, width=50, borderwidth=3, bg="Light Grey", fg="black")
e.pack()
#Here is the part that is not working.
img3 = PhotoImage(file=r"C:\\Tkinter\\ComputerImage.png")
picture1 = Label(window2, image=img3)
picture1.pack()
SaveAndContinue = Button(window2, text="Save and Return to Main Menu", padx=75, pady=20, bg="Light Grey")
SaveAndContinue.pack()
Try the answer from josav09 for this question:
How to add an image in Tkinter?
from tkinter import *
from PIL import ImageTk, Image
import os
root = Tk()
img = ImageTk.PhotoImage(Image.open("True1.gif"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
Related
I am trying to resize a window based on how much text there is/are in a/some label(s). Here is what I mean: https://imgur.com/a/kBb1xaQ. I want this window to get as big to fit all the text inside.
I have been looking for parameters from the Label from tkinter but nothing seemed to work. Some help would be much appreciated, thank you.
This is some of the code I have done:
import tkinter
from tkinter import *
header = 'Staff requirements'
text = ['bla bla', 'bla bla']
window = Tk()
window.title('{}'.format(header.capitalize()))
window.geometry('400x200') # 80 + (x*15)
title_frame = Frame(window, borderwidth=2, relief='solid')
title_frame.pack(pady=10)
title_lbl = Label(title_frame, text=header.capitalize(), padx=5, pady=5, borderwidth=1, relief='solid', font=('Calibri', 15))
title_lbl.pack()
content_frame = Frame(window, borderwidth=1, relief='solid')
content_frame.pack(pady=10, padx=10, fill='y')
for sub_text in text:
content_lbl = Label(content_frame, text=sub_text, padx=5, pady=5, wraplength=360, justify='left', font=('Calibri', 10))
content_lbl.pack()
Change:
window.geometry('400x200')
to :
window.geometry('')
Also , if you want to prevent the user from resizing the window , you can try :
window.resizable(False,False)
I believe you are trying to find the Entry method, be careful with the geometry...
import tkinter as tk
guiApp= tk.Tk()
guiApp.geometry("630x630")
textPlace= tk.Entry(guiApp)
textPlace.place(x = 10,y = 10,width=600,height=600)
guiApp.mainloop()
I want to change the fontsize in a Tkinter button. But the button size changes along with it.
I even tried to limit the height and width of the button but nothing seems to work,
This is the code:
from tkinter import *
root = Tk()
root.geometry("500x300")
root.resizable(False, False)
button = Button(root, text="Ihsan", bg="Black", fg="white",
activeforeground="white", activebackground="grey", width=15, height=3,
font=("ariel", "43"))
button.place(x=350, y=20)
root.mainloop()
I get a window with a huge button. Please help
Width and height of the button in letters (if displaying text) or pixels (if displaying an image).
Here's one way to go, by using size of image to limit size of button when font size changed. Not sure if it is best one.
import tkinter as tk
from PIL import Image, ImageTk
def new_font():
global setting
setting = 1 - setting
font = f"ariel {32 if setting else 16}"
button.configure(font=font)
setting = 0
root = tk.Tk()
im = Image.new("RGB", (200, 200))
photo = ImageTk.PhotoImage(im)
root.geometry("300x300")
root.resizable(False, False)
button = tk.Button(
root,
text="Ihsan",
bg="Black",
fg="white",
activeforeground="white",
activebackground="grey",
width=200,
height=200,
font=("ariel", "16"),
image=photo,
compound='center')
button.place(x=30, y=20)
size = tk.Button(root, text="Size", command=new_font)
size.place(x=30, y=250)
root.mainloop()
The width and height is what is giving you the large button. Height in this case means 3 fontsize high.
Default is that it only enclose the text.
Margins you provide with pad, either in the widget itself, or when you pack it.
Using place is a bit uncommon and has its specific usecases, it does not look like you need it. The pack geometry manager can be used instead.
from tkinter import *
root = Tk()
root.geometry("500x300")
root.resizable(False, False)
button = Button(root, text="Ihsan", bg="Black", fg="white",
activeforeground="white", activebackground="grey",# width=15, height=3,
font=("ariel", "43"))
#button.place(x=350, y=20)
button.pack(side=RIGHT)
root.mainloop()
I need a python messagebox with custom buttons.
I need something like this. I need it to return the button clicked. Like If I clicked 'A' it would return 'A'
I know about tkinter but I want to use this with pygame and can't get it to work.
Here is what I made with tkinter, not the perfect messagebox, but why not?
For the theme used you have to install it first, like:
pip install ttkthemes
Then the code
# imports
from tkinter import *
from tkinter import ttk
from ttkthemes import themed_tk as tktheme
from PIL import ImageTk, Image
from tkinter import messagebox
# making new themed window
root = tktheme.ThemedTk()
root.title('Take selection')
root.get_themes()
root.set_theme('vista')
# message to be shown by the box
message = 'Here is a custom messagebox.'
# defining functions
def click1():
root.destroy()
return 'A'
def click2():
root.destroy()
return 'B'
def click3():
root.destroy()
return 'C'
# creating white frame
frame1 = Frame(height=139, width=440, bg='white')
frame1.grid(row=0, column=0)
# creating gray frame
frame2 = ttk.Frame(height=50, width=440)
frame2.grid(row=1, column=0)
# importing the image, any image can be used(for the question mark)
dir = Image.open('Blue_question.png')
dir = dir.resize((50, 50), Image.ANTIALIAS)
img_prof = ImageTk.PhotoImage(dir)
img_label = Label(root, image=img_prof, bg='white')
img_label.grid(row=0, column=0, sticky=W, padx=25)
# defining main label
cust_messagebox = Label(root, text=message, font=('Arial', 10), bg='white')
cust_messagebox.grid(row=0, column=0, sticky=W, padx=(95, 0))
# defining buttons
button1 = ttk.Button(root, text='A', command=click1)
button1.grid(row=1, column=0, sticky=W, padx=30, ipadx=10)
button2 = ttk.Button(root, text='B', command=click2)
button2.grid(row=1, column=0, ipadx=10)
button3 = ttk.Button(root, text='C', command=click3)
button3.grid(row=1, column=0, sticky=E, padx=(0, 30), ipadx=10)
# keeping the app alive
root.mainloop()
With bigger messages you might want to increase the width of the frames and the play with the padding of the widgets too.
If any doubts or errors, do let me know.
Cheers
I am trying to add a background image to my GUI app using tkinter. In the root window, I have two frames, named container_1 and container_2. When I am adding the background image, these framed are pushed outside the image. I can see them as the window size is higher than image size. If it is less, it isn't even visible. How can I make them place over the background image? Is it possible to give different background images for container_1 and container_2?
ans suggestions please.
(imgae in the comment link)
import tkinter as tk
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('800x600')
#bg image
background_image=tk.PhotoImage(file= "bgPic.png")
background_label = tk.Label(root, image=background_image)
background_label.pack() # is pack problematic here?
container_1 = tk.Frame(root, borderwidth=2, relief="solid")
container_2 = tk.Frame(root, borderwidth=2, relief="solid")
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()
#setting background image
background_main_img=tk.PhotoImage(file= "bgMain.png")
background_main = tk.Label(root, image=background_main_img)
background_main.place(relx=.5, rely=.5, anchor="center")
#setting background image for container frame
background_con_1=tk.PhotoImage(file= "bgContainer.png")
background_label = tk.Label(container_1, image=background_con_1)
background_label.place(relx=.5, rely=.5, anchor="center")
I'm relatively new to Tkinter and I need help.
I have created a new window when a button is clicked from the parent window. The new window is the def new_Window. But I can't seem to get the information in the window as coded below:
from tkinter import *
from tkinter import ttk
#User Interface Code
root = Tk() #Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel(root)
display = Label(newWindow, width=200, height=50)
message = Label(root, text="Welcome")
display.pack()
message.pack()
display2 = Label(root, width=100, height=30)
button1 = Button(root, text ="Continue", command=new_window, width=16,
bg="red")
message_label = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
username = StringVar() #Stores the username in text
user_entry = Entry(root, textvariable=username) #Creates an entry for the
username
user_entry.pack()
display2.pack()
button1.pack()
message_label.pack()
root.mainloop()#Runs the main window loop
Thanks for your help.
You did not pack the hello label into the new window. A tip is also to use background colors to visualize labels when developing. Here is a functioning code for you. I only changed 2 lines, added foreground and background.
from tkinter import *
from tkinter import ttk
# User Interface Code
root = Tk() # Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel(root)
display = Label(newWindow, width=200, height=50,bg='RED')
message = Label(newWindow, text="HEEEY",fg='BLACK',bg='GREEN')
message.pack()
display.pack()
display2 = Label(root, width=100, height=30)
button1 = Button(root, text ="Continue", command=new_window, width=16,
bg="red")
message_label = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
username = StringVar() # Stores the username in text
user_entry = Entry(root, textvariable=username) # Creates an entry for the
username
user_entry.pack()
display2.pack()
button1.pack()
message_label.pack()
root.mainloop() # Runs the main window loop