I'm creating this program that it's not complete yet, a small detail that annoys me, when you run the program and click "OPEN" it creates a new Toplevel window inside the def click_selettore function in the beginning, and i notice that the black screen on the left "blinks" for a split second, for a split second is white and then it become black i hope you notice this, the photo attached it's a frame of the Toplevel opening window and i notice the white frames that are loading i think, and they are white for a split second. How to solve this and not making the new window white for the split second? Thanks, here is the code it's a bit long but the probelm is only the white blinks when creating the window inside the "click selettore" function, if you help me i would really appreciate it
from tkinter import *
import customtkinter
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
def click_selettore(event):
bg = "#414141"
win.attributes('-topmost', 0)
win2 = Toplevel(win)
win2.focus()
win2.resizable(False,False)
win2.grab_set()
win2.configure(bg=bg)
win2.title("Selettore colori")
frame_schermo = Frame(win2)
frame_schermo.grid(row=0,column=0,padx=(20,0),pady=(20,0))
schermo = Label(frame_schermo, bg="#000000", width=45, height=9, relief="solid", borderwidth=3)
schermo.grid(row=0,column=0)
frame_slider = Frame(win2,bg=bg)
frame_slider.grid(row=0,column=1,sticky=N,pady=(20,0),padx=(50,0))
etc_rosso = Label(frame_slider, text="Rosso", bg=bg, fg="#C70000", font=("Ink Free", 20, "bold"))
etc_rosso.grid(row=0,column=0,pady=(11,0))
slider_rosso = customtkinter.CTkSlider(frame_slider, from_=0, to=255,width=230,progress_color='#C70000',fg_color='#a34949',button_hover_color='#3f9feb',button_color='#1f82d1')
slider_rosso.grid(row=0,column=1,sticky=N,padx=(10,0),pady=(23,0))
slider_rosso.set(0)
casella_rosso = customtkinter.CTkEntry(master=frame_slider,text_font=('Courier',15,'bold'),width=55)
casella_rosso.grid(row=0,column=2,pady=(11,0),padx=(7,20))
casella_rosso.insert(0,0)
etc_verde = Label(frame_slider, text="Verde", bg=bg, fg="#3fc441", font=("Ink Free", 20, "bold"))
etc_verde.grid(row=1,column=0,pady=(11,0))
slider_verde = customtkinter.CTkSlider(frame_slider, from_=0, to=255, width=230, progress_color='#3fc441', fg_color='#3d803e', button_hover_color='#3f9feb', button_color='#1f82d1')
slider_verde.grid(row=1, column=1, sticky=N,padx=(10,0),pady=(23,0))
slider_verde.set(0)
casella_verde = customtkinter.CTkEntry(master=frame_slider, text_font=('Courier', 15, 'bold'), width=55)
casella_verde.grid(row=1, column=2,pady=(11,0),padx=(7,20))
casella_verde.insert(0, 0)
etc_blu = Label(frame_slider, text="Blu", bg=bg, fg="#010178", font=("Ink Free", 20, "bold"))
etc_blu.grid(row=2,column=0,pady=(11,0))
slider_blu = customtkinter.CTkSlider(frame_slider, from_=0, to=255, width=230, progress_color='#010178',fg_color='#222275', button_hover_color='#3f9feb', button_color='#1f82d1')
slider_blu.grid(row=2, column=1, sticky=N,padx=(10,0),pady=(23,0))
slider_blu.set(0)
casella_blu = customtkinter.CTkEntry(master=frame_slider, text_font=('Courier', 15, 'bold'), width=55)
casella_blu.grid(row=2, column=2,pady=(11,0),padx=(7,20))
casella_blu.insert(0, 0)
frame_sotto_slider = Frame(win2,bg=bg)
frame_sotto_slider.grid(row=1,column=0,pady=(45,0),padx=(35,0))
etc_esadecimale_titolo= Label(frame_sotto_slider,text='Esadecimale -> ',font=('Courier',16),fg='white',bg=bg)
etc_esadecimale_titolo.grid(row=0,column=0)
campo_esadecimale = customtkinter.CTkEntry(master=frame_sotto_slider,text_font=('Courier',16),width=105)
campo_esadecimale.grid(row=0,column=1,padx=(6,0))
btn_ok = customtkinter.CTkButton(win2,text='Ok',text_font=('Courier',16),width=50,corner_radius=10,hover_color='#3a76a6')
btn_ok.grid(row=3,column=1,sticky=E,pady=(50,10),padx=(0,10))
win.eval(f'tk::PlaceWindow {str(win2)} center')
win2.mainloop()
win = Tk()
win.configure(bg="#2b2b2b")
etichetta_selettore_colori = Label(win,text='OPEN',relief="solid",borderwidth=4,font=('Helvetica',17))
etichetta_selettore_colori.grid(row=0,column=0,padx=(20,0),pady=20)
etichetta_selettore_colori.bind("<Button-1>",click_selettore)
win.eval('tk::PlaceWindow . center')
win.mainloop()
Related
i am currently making an app which displays information about NBA players using an API called data.nba.net, i am trying to display information such as a player's last affiliation, height, jersey number, etc. But at some point in the process the labels cut off and it stops showing the rest of them.
I've tried messing around with the dimensions of both the window and the canvas, but it doesn't seem to solve the problem. I am not an expert on tkinter, so i am not sure how do canvases and frames work, pls help :,).
What it should look like
What it looks like at the end
from tkinter import *
from turtle import position
from weakref import WeakSet
import nba_now
from nba_now import *
from tkinter import ttk
main_window = Tk()
main_window.geometry("420x420")
main_window.title("NBA NOW")
def players_windowP():
n = 0.030
win = Tk()
win.title('Player Info')
win.geometry("820x520")
win.resizable(False, False)
wrapper1= LabelFrame(win)
mycanvas = Canvas(wrapper1, width=800, height=520)
mycanvas.pack(side=LEFT)
yscrollbar = ttk.Scrollbar(wrapper1, orient="vertical", command= mycanvas.yview)
yscrollbar.pack(side=RIGHT, fill="y")
mycanvas.configure(yscrollcommand=yscrollbar.set)
mycanvas.bind('<Configure>', lambda e: mycanvas.configure(scrollregion = mycanvas.bbox('all')))
myframe = Frame(mycanvas)
mycanvas.create_window((0,0), window=myframe, anchor="nw")
wrapper1.pack(fill="both", expand="yes", padx=0, pady=0)
stats = get_links()['leagueRosterPlayers']
players = get(BASE_URL + stats).json()['league']['standard']
for player in players:
fname = player['firstName']
lname = player['lastName']
jersey = player['jersey']
pos = player ['pos']
height = player['heightFeet']
team = player['lastAffiliation']
data = Label(myframe, text=f"{fname} {lname} - {team}\n Number: {jersey}\n Height: {height} feet\n Position: {pos}",
font=('Arial', 25, 'bold'),
relief = RIDGE,
bd=6,
padx=0,
pady=30,
borderwidth= 5,
justify= LEFT)
data.place(relx= 0, rely= n, anchor=N)
n+= 0.45
data.pack()
players = Button(main_window, text= "Player Info",
font=('Arial', 12, 'bold'),
justify=CENTER,
state=ACTIVE,
bd=1,
command=players_windowP)
players.place(relx=0.5, rely=0.8, anchor=S)
main_window.mainloop()
from tkinter import *
import turtle
def RunTurtle():
import turtle
window = turtle.Screen()
window.bgcolor("light green")
window.title("Turtle")
t = turtle.Turtle()
t.speed(20)
def MyTurtleFunc(Fl, Sl, f, rin, ro, c, w):
t.color(c)
t.width(w)
for _ in range(Fl):
for _ in range(Sl):
t.forward(f)
t.right(rin)
t.right(ro)
MyTurtleFunc(36, 4, 100, 90, 10, "blue", 4)
def RunGUI():
master = Tk()
master.title("Project")
canvas = Canvas(master, height=1080, width=1920)
canvas.pack()
def RUN():
RunTurle()
TopFrame = Frame(master, bg='light green')
TopFrame.place(relx=0.04, rely=0.04, relwidth=0.91, relheight=0.6)
MiddleFrame = Frame(master, bg='light blue')
MiddleFrame.place(relx=0.04, rely=0.66, relwidth=0.91, relheight=0.06)
BottomFrame = Frame(master, bg='orange')
BottomFrame.place(relx=0.04, rely=0.74, relwidth=0.91, relheight=0.25)
TopLabel = Label(TopFrame, bg='light green', text="Drawing Robot", font="calibri 15 bold", foreground="black")
TopLabel.pack(padx=5, pady=5)
LabelBottom = Label(BottomFrame, bg='orange', text="Debug Area ", font="vendara 15 bold ", foreground="black")
LabelBottom.pack(padx=0.1, pady=0.1)
LabelBottomText = Text(BottomFrame, height=10.5, width=180)
LabelBottomText.tag_configure('style', foreground='grey', font=('calibri', 10, 'bold'))
LabelBottomText.pack()
fronttext = "..."
LabelBottomText.insert(END, fronttext, 'style')
RunProgram = Button(MiddleFrame, text="RUN PROGRAM", foreground="black", command=RUN)
RunProgram.pack(padx=0.2, pady=0.2, side=LEFT)
master.mainloop()
I designed interface my entry level program.I divide this three part.TopFrame must show turtle.Middle Frame has button which help us to start the program .Bottom Frame has debug area. I want to integrate "RunTurtle()" function into TopFrame.When the user click "RUN PROGRAM" button ,interface will open and turtle will draw pattern into TopFrame.How can I do this ? thank you...
def RUN():
RunTurle()
should be
def RUN():
RunTurtle()
and run the function RunGUI() its works
I am attempting to create a GUI that allows for users to copy and paste data straight into the interface. There will be three columns and anywhere between 1 - 500 rows with the possibility of 3,000+ The raw text will come via email, word, PDF, or excel so I need the user input field to be similar to excel in that you can copy/paste more than 1 line at a time.
I have followed a couple YouTube guides on creating tables/grids with tkinter however I cannot figure out a way to allow text entry via copy/paste.
One of the only ways I found to make tables with tkinter since tktable isn't a part of it is the following example. This still does not allow for user input via copy/paste.
class Table:
def __init__(self,root):
# code for creating table
for i in range(total_rows):
for j in range(total_columns):
self.e = Entry(root, width=20, fg='blue',
font=('Arial',16,'bold'))
self.e.grid(row=i, column=j)
self.e.insert(END, lst[i][j])
# take the data
lst = [(1,'Raj','Mumbai',19),
(2,'Aaryan','Pune',18),
(3,'Vaishnavi','Mumbai',20),
(4,'Rachna','Mumbai',21),
(5,'Shubham','Delhi',21)]
# find total number of rows and
# columns in list
total_rows = len(lst)
total_columns = len(lst[0])
# create root window
root = Tk()
t = Table(root)
root.mainloop()
Can anyone suggest an alternative to tkinter or point me in the direction of a guide for this type of user input?
I finally figured out why I couldn't get python to allow me to use tkinter and was able to do what I was trying to do, more or less.
import tkinter as tk
from tkinter import *
from tkinter import BOTH, END, LEFT
HEIGHT = 970
WIDTH = 1500
root = tk.Tk()
root.title( 'Daves Generator')
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
background_image = tk.PhotoImage(file='david.png')
background_label = tk.Label(root, image=background_image)
background_label.place(relwidth=1, relheight=1)
frame_one = tk.Frame(root, bg='#003d70',bd=5)
frame_one.place(relx=0.17, rely=.15, relwidth=0.3,relheight=0.8, anchor='n')
customer_description = tk.Text(frame_one, state=NORMAL, width=125, wrap=WORD, height=500,font=("Courier", 8))
customer_description.place(relwidth=1, relheight=1)
frame_two = tk.Frame(root, bg='#003d70',bd=5)
frame_two.place(relx=0.5, rely=.15, relwidth=0.3,relheight=0.8, anchor='n')
dave_description = tk.Text(frame_two, width=125, height=500,font=("Courier", 8))
dave_description.place(relwidth=1, relheight=1)
frame_three = tk.Frame(root, bg='#003d70',bd=5)
frame_three.place(relx=0.83, rely=.15, relwidth=0.3,relheight=0.8, anchor='n')
sap_code = tk.Label(frame_three, width=125, height=500,font=("Courier", 8))
sap_code.place(relwidth=1, relheight=1)
header_frame_one = tk.Label(root, text="Customer Descriptions", font=("Courier", 14), fg='#003d70')
header_frame_one.place(relx=0.17, rely=0.13, relwidth=0.2, relheight=0.025, anchor='n')
header_frame_two = tk.Label(root, text="PJ Descriptions", font=("Courier", 14), fg='#003d70')
header_frame_two.place(relx=0.5, rely=0.13, relwidth=0.2, relheight=0.025, anchor='n')
header_frame_three = tk.Label(root, text="SAP Code", font=("Courier", 14), fg='#003d70')
header_frame_three.place(relx=0.83, rely=0.13, relwidth=0.2, relheight=0.025, anchor='n')
generate_button = tk.Button(root,text="Generate!", font=("Courier", 14), fg='white', bg='#003d70', command=lambda: get_customer_description())
generate_button.place(relx=0.5, rely=0.08, relwidth=0.15, relheight=0.025, anchor='n')
I'm creating a simple madlib style game and I've come into a bit of a problem. I cannot get the canvas to clear and show the results.
The following code places an image as the background of a canvas. It then places labels and entry fields in 2 columns for all of the words to be inserted. There is a submit button at the bottom of the page. I can't figure out how to get it clear everything except the background image, so that it can display the story, with the users words inserted. If i place it in the callback(), it clears just the background and keeps everything else. I want the opposite.
from tkinter import *
from PIL import Image, ImageTk
canvas_width = 360
canvas_height = 525
file = r"C:\Users\kraak\Desktop\PyCharm Community Edition 2017.1.2\borderedpaper.GIF"
master = Tk()
canvas = Canvas(master, width=canvas_width, height=canvas_height)
old_img = PhotoImage(file=file)
new_img = old_img.subsample(3, 3)
canvas.create_image(-11, -10, anchor=NW, image=new_img)
canvas.create_window(0, 0, height=1, width=1, anchor=NW)
canvas.create_text(0, 0, text="Test")
e1 = Entry(canvas)
canvas.create_window(250, 60, window=e1, height=15, width=100)
label = Label(text="Enter an adjective.")
label.place(x=40, y=50)
e1.focus_set()
e2 = Entry(canvas)
canvas.create_window(250, 85, window=e2, height=15, width=100)
label = Label(text="Enter a nationality.")
label.place(x=40, y=75)
e2.focus_set()
def callback():
print("Pizza was invented by a " + (e1.get()) + " " + (e2.get()))
def answer():
button = Button(text="Submit.", command=callback)
button.place(x=150, y=460)
answer()
canvas.pack()
mainloop()
As Bryan Oakley suggested you can store the id's of the widgets you want to get rid of in a list to make it easier to destroy() them all in the callback() function. Here's showing the modification to your code that would do that—note the lines with a # ADDED comments.
from tkinter import *
from PIL import Image, ImageTk
canvas_width = 360
canvas_height = 525
file = r"C:\Users\kraak\Desktop\PyCharm Community Edition 2017.1.2\borderedpaper.GIF"
master = Tk()
canvas = Canvas(master, width=canvas_width, height=canvas_height)
canvas_entry_widgets = [] # ADDED
old_img = PhotoImage(file=file)
new_img = old_img.subsample(3, 3)
canvas.create_image(-11, -10, anchor=NW, image=new_img)
canvas.create_window(0, 0, height=1, width=1, anchor=NW)
canvas.create_text(0, 0, text="Test")
e1 = Entry(canvas)
canvas.create_window(250, 60, window=e1, height=15, width=100)
label = Label(text="Enter an adjective.")
label.place(x=40, y=50)
e1.focus_set()
canvas_entry_widgets.append(e1) # ADDED
e2 = Entry(canvas)
canvas.create_window(250, 85, window=e2, height=15, width=100)
label = Label(text="Enter a nationality.")
label.place(x=40, y=75)
e2.focus_set()
canvas_entry_widgets.append(e2) # ADDED
def callback():
print("Pizza was invented by a " + (e1.get()) + " " + (e2.get()))
# destroy the canvas entry widgets and clear the list # ADDED
while canvas_entry_widgets: # ADDED
widget = canvas_entry_widgets.pop() # ADDED
widget.destroy() # ADDED
def answer():
button = Button(text="Submit.", command=callback)
button.place(x=150, y=460)
answer()
canvas.pack()
mainloop()
Every widget has a destroy method which can be used to delete the widget. In your callback you can simply call this method for every widget:
def callback():
e1.destroy()
e2.destroy()
...
In your specific case, if you want to delete all the labels you will have to give them unique names. Or, to make this even easier, you can store all of your widgets and iterate over the list.
I see the text if I enlarge the window manually, so the text is there! (On the label) but if not enlarged the window I can not see, I see only a piece of the label
How can I fix it? Do I have to resize?
I've tried changing the font size, the size of the label and some other things and I could not even solve the problem.
This is the code:
from Tkinter import *
import ttk
import time
import threading
def start():
start_stop.config(text="QUIT", command=stop, image=photo2)
thread = threading.Thread(target=progBar, args=())
thread.daemon = True
thread.start()
def progBar():
for i in xrange(300):
if i < 50:
lbl1.config(height=0, width=4, font=('times', 400, 'bold'),
text="AAAA!!")
lbl1.config(fg='black')
if i % 5:
lbl1.config(bg='white')
else:
lbl1.config(bg='red')
if i == 50:
lbl1.destroy()
if i >= 200:
s = ttk.Style()
s.theme_use('clam')
if i % 5:
s.configure("red.Horizontal.TProgressbar",
foreground='#205F8C', background='#205F8C')
else:
s.configure("red.Horizontal.TProgressbar",
foreground='red', background='red')
pbar_det.config(style="red.Horizontal.TProgressbar")
pbar_det.step(0.33)
master.update()
# Busy-wait
time.sleep(0.1)
master.destroy()
def stop():
master.destroy()
master = Tk()
photo1 = PhotoImage(file="Press1.gif")
photo2 = PhotoImage(file="Press2.gif")
ws = master.winfo_screenwidth() # width of the screen
hs = master.winfo_screenheight() # height of the screen
width = ws
height = 120
x = ws-width # Window's coords
y = hs-height
start_stop = Button(master, text='START', command=start, image=photo1)
start_stop.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)
pbar_det = ttk.Progressbar(master, orient="horizontal", length=ws-450,
mode="determinate")
pbar_det.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S)
lbl1 = Label(master)
lbl1.grid(row=0, column=1, pady=2, padx=2)
lbl2 = Label(master)
lbl2.grid(row=0, column=1, pady=2, padx=2)
master.geometry('%dx%d+%d+%d' % (width, height, x, y))
master.mainloop()
Try anchoring the text within the label to the left.
lbl1.config(anchor="nw") # nw = north-west = top left
I see at least four problems in the code:
you are forcing the size of the widget to a specific height when you call master.geometry(...). This prevents the window from growing or shrinking to fit the internal widgets
you are specifying a really huge font (400 points) which can't fit in the height you've picked. Because of the huge size of the font relative to the space you've given it (by explicitly setting the height of the window) what you see is the natural whitespace above the letters that is present in all fonts).
you are putting two labels and one button on top of each other in the same row and column
you are using threading. Tkinter isn't thread safe. Your code might work, but it might not.
If you remove the call to master.geometry(), the window will expand to fit the height of the text. Or, you can not make the font so huge.