Second Label not showing up, Python Tkinter - python

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

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 ?

How to center text in text widget in tkinter

I have a text widget
from tkinter import *
root = Tk()
root.wm_title("cloth")
root.geometry("500x500")
l = Text(root, width=46, bg="White",relief = "solid", font="Arial 9",height=1,bd=1)
l.configure(state='normal')
l.configure(state='disabled')
l.grid(row=1, column=1)
I want to center text
but if I select then only the text is selected, no space is selected
You need add tag_configure("tag_name", justify='center') and tag_add("tag_name", "1.0", "end")
Here is the example
from tkinter import *
root = Tk()
root.wm_title("cloth")
root.geometry("500x500")
l = Text(root, width=46, bg="White",relief = "solid", font="Arial 9",height=1,bd=1)
l.tag_configure("tag_name", justify='center')
l.insert("1.0", "How to center text in text widget in tkinter ?")
l.tag_add("tag_name", "1.0", "end")
l.grid(row=1, column=1)
I think, no way to do it without adding spaces
import tkinter as tk
root = tk.Tk()
T1 = tk.Text(root)
T1.tag_configure("center", justify='center')
T1.insert(1.0, " ")
T1.tag_add("center", "1.0", "end")
T1.pack()
root.mainloop()

Give the ability to user to copy text in tkinter gui

In this code when the user enters the link, a short version of the link get displayed but it does not give the ability to the user to copy the link from the GUI. How do i fix this?
import pyshorteners as pr
from tkinter import *
root = Tk()
e = Entry(root, width=50)
e.pack()
def click():
link = e.get()
shortener = pr.Shortener()
Short_Link = shortener.tinyurl.short(link)
Label1 = Label(root, text=Short_Link)
Label1.pack()
Button1 = Button(root, text="Enter link:", command=click)
Button1.pack()
root.mainloop()
You can't directly copy the text from a Tkinter Label widget with CTRL+C.
This is a simple Tkinter app to copy the text of a Label to the clipboard:
from tkinter import *
from tkinter.messagebox import showinfo
class CopyLabel(Tk):
def __init__(self, text: str):
super(CopyLabel, self).__init__()
self.title('Copy this Label')
self.label_text = text
self.label = Label(self, text=text)
self.label.pack(pady=10, padx=40)
self.copy_button = Button(self, text='COPY TO CLIPBOARD', command=self.copy)
self.copy_button.pack(pady=5, padx=40)
def copy(self):
self.clipboard_clear()
self.clipboard_append(self.label_text)
self.update()
showinfo(parent=self, message='Copied to clipboad!')
if __name__ == "__main__":
app = CopyLabel('Copy me!')
app.mainloop()
In your code to copy automatically the Short_Link you can do:
import pyshorteners as pr
from tkinter import *
root = Tk()
e = Entry(root, width=50)
e.pack()
def click(master: Tk):
link = e.get()
shortener = pr.Shortener()
Short_Link = shortener.tinyurl.short(link)
master.clipboard_clear()
master.clipboard_append(Short_Link)
master.update()
Label1 = Label(root, text=Short_Link)
Label1.pack()
Button1 = Button(root, text="Enter link:", command=lambda: click(root))
Button1.pack()
root.mainloop()

Why does Tkinter make a new window for my button?

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.

Tkinter new window

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

Categories