Why does Tkinter make a new window for my button? - python

Im trying to add a button into my window, but it makes a new window for the button, how can I add it to the same window where I have a background on?
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import messagebox
from PIL import ImageTk
top = Tk()
C = Canvas(top, bg ="blue", height=1920, width=1080)
filename = ImageTk.PhotoImage(file = "C:/Users/plapl/Desktop/ching.jpg")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
root = Tk()
myButton = Button(root, text = "Add a task")
myButton.pack()
root.mainloop()

It's because you have created a new instance for Tk().
change it to this:
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import messagebox
from PIL import ImageTk
top = Tk()
C = Canvas(top, bg ="blue", height=1920, width=1080)
filename = ImageTk.PhotoImage(file = "C:/Users/plapl/Desktop/ching.jpg")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
myButton = Button(top, text = "Add a task")
myButton.pack()
root.mainloop()

This happens because you have 2 Tk() objects, which means that 2 windows are created. To solve this, change the line: background_label = Label(top, image=filename) to this: background_label = Label(root, image=filename).
Then, you can get rid of this line: top = Tk() because it isn't needed anymore.

Related

Python - Custom tkinter - component.setfocus() don't work with customtkinter component like CTkEntry

I have this code and focus dont work with customtkinter
from tkinter import *
import customtkinter as ctk
import sys
if len(sys.argv) > 1:
print("Tk Customized from Tom")
win = ctk.CTk()
win.geometry("750x250")
text = ctk.CTkTextbox(win, width=200, height=200)
text.pack()
text.insert(INSERT, "Hello World!")
entry = ctk.CTkEntry(win, width=100)
entry.pack()
entry.focus_set()
else:
print("Standard Tk ")
win = Tk()
win.geometry("750x250")
text = Text(win, width=30, height=10)
text.insert(INSERT, "Hello World!")
text.pack()
entry = Entry(win, width=30)
entry.pack()
entry.focus_set()
win.mainloop()
py testy.py -> Start standard Tkinter Focus is on Entry
py testy.py 1 -> Start with customtkinter objects Focus is NOT on Entry
How can get focus on CTkEntry ?

Second Label not showing up, Python Tkinter

I am trying show the color that is picked with my_label2 but its not showing up. (And im not getting an error message)
from tkinter import *
from tkinter import colorchooser
root = Tk()
root.title("Color Picker by Hamza")
root.geometry("400x400")
def color():
my_color = colorchooser.askcolor()
my_color2 = colorchooser.askcolor()[1]
my_label = Label(root, text=my_color).pack(pady=10)
my_label2 = Label(root, text=my_color2, font=("Helvetica, 32"), bg=my_color).pack()
my_button = Button(root, text="Pick A Color", command=color).pack()
root.mainloop()
Thats how it should look like:
screenshot1
This is how it looks like:
screenshot2
you should write my_color2 in label bg
try this:-
from tkinter import *
from tkinter import colorchooser
root = Tk()
root.title("Color Picker by Hamza")
root.geometry("400x400")
def color():
my_color = colorchooser.askcolor()
my_color2 = my_color[1]
my_label = Label(root, text=my_color).pack(pady=10)
my_label2 = Label(root, text=my_color2, font=("Helvetica, 32"), bg=my_color2).pack()
my_button = Button(root, text="Pick A Color", command=color).pack()
root.mainloop()

Tkinter Image not showing up

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

How do i place a background image on tab window in tkinter

i wanted to know how to place a background image on Tkinter tab window i usually get an error.please help
Main Window:
from tkinter import *
gui = Tk()
gui.geometry('100x100')
C = Canvas(gui, bg="blue", height=250, width=300)
filename = PhotoImage(file='example.png')
filename = filename.zoom(7)
filename = filename.subsample(18)
background_label = Label(gui, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
gui.mainloop
Or
New Tab Window:
from tkinter import *
gui = Tk()
gui.geometry('100x100')
def Tab():
gui = Toplevel()
gui.geometry('100x100')
C = Canvas(gui, bg="blue", height=250, width=300)
filename = PhotoImage(file='example.png')
filename = filename.zoom(7)
filename = filename.subsample(18)
background_label = Label(gui, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
Tab()
gui.mainloop()
You Also Need To Have Your Image In The Defined Directory.
Hope It Helps & Welcome To StackOverflow!

Tkinter in fullscreen mode makes Text widget not work

When using Tkinter as a regular window, the text widget works as intended and can be typed in.
from Tkinter import *
root = Tk()
T = Text(root, height=2, width=30)
T.pack()
T.insert(END, "Just a text Widget\nin two lines\n")
mainloop()
However if we make this application fullscreen:
from Tkinter import *
root = Tk()
root.attributes("-fullscreen", True)
T = Text(root, height=2, width=30)
T.pack()
T.insert(END, "Just a text Widget\nin two lines\n")
mainloop()
And try to type in the textbox, it won't work and the keys will instead appear in the terminal. Why is this and is there a solutoin?
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes('-fullscreen',True)
canvas= Canvas(root)
canvas.create_text(300, 50,text="This is a window", fill="black", font=('Helvetica 15 bold'))
canvas.pack()
This seems to be working with me

Categories