put label in frame without class etc - python-2.7

How can I get the label in this simple frame? I tried to make the label go to the frame but then I only see the label and the frame disappears.
Notice here that I want 2 different sizes. The root.geometry("200x200") which is gray. I also am putting a Frame in the root of 100w x 100h. Run the code, I can see both color areas great. But my label does not appear, in the Frame darkgray area. Which is my problem.
from Tkinter import *
root=Tk()
root.geometry("200x200")
root.title("basicGUI")
root.configure(background="gray")
frame = Frame(width=100, height=100,
bg="darkgray")
frame.pack()
mylabel=Label(text="anyWord")
mylabel.pack()
"""
# darkgray frame disappears
mylabel=Label(frame, text="anyWord")
mylabel.pack(side = LEFT)
"""
mainloop()

Thanks to Lafexlos, a one line addition solved my problem.
from Tkinter import *
root=Tk()
root.geometry("200x200")
root.title("basicGUI")
root.configure(background="gray")
frame = Frame(width=100, height=100,
bg="darkgray")
frame.pack_propagate(0) #adding line solves problem
frame.pack()
mylabel1=Label(frame, text="anyWord")
mylabel1.pack(side = TOP)
mainloop()

Related

Tkinter Background not showing transparent

Hey im a newbie in python in my code there is a animated gif that play whenever i run the code. The gif image is actually transparent when open in adobe etc. but the problem is when i run the code.The frame which is the color gray included in the gif.I want to remove the color gray so that the only thing that can see is my only animated gif
This is my code:
# mimic an animated GIF displaying a series of GIFs
# an animated GIF was used to create the series of GIFs
# with a common GIF animator utility
import time
from Tkinter import *
root = Tk()
imagelist = ["dog001.gif","dog002.gif","dog003.gif"]
# extract width and height info
photo = PhotoImage(file=imagelist[0])
width = photo.width()
height = photo.height()
canvas = Canvas(width=width, height=height)
canvas.create_line(0,240,640,240, fill='blue')
canvas.pack()
# create a list of image objects
giflist = []
for imagefile in imagelist:
photo = PhotoImage(file=imagefile)
giflist.append(photo)
# loop through the gif image objects for a while
for k in range(0, 10):
for gif in giflist:
canvas.delete(ALL)
canvas.create_image(width/2.0, height/2.0, image=gif)
canvas.update()
time.sleep(0.1)
root.mainloop()[![enter image description here][1]][1]
dog001.gif
dog002.gif
dog003.gif
As described here, making a Tkinter window transparent is possible, but depends on your OS.
If you are on Windows, just add the following lines after creating the root:
root = Tk()
# Hide the root window drag bar and close button
root.overrideredirect(True)
# Make the root window always on top
root.wm_attributes("-topmost", True)
# Define a transparent color
root.wm_attributes("-transparentcolor", '#eeefff')
Then set your canvas' background color as the transparent color defined above:
canvas = Canvas(width=width, height=height, bg='#eeefff', highlightthickness=0)

Tkinter - create flashing graphic on a canvas?

I'm using Tkinter (2.7) to try to create a finite loop that draws, then erases a rectangle on a canvas widget, like its flashing. After several days of trying everything I could find, I have to ask for help.
The problem:
The code below seems to create and delete the rectangle inside the program but not display it in the main window (root).
from Tkinter import *
root = Tk()
def make():
canvas.create_rectangle(20,20,60,60,fill="pink")
root.after(1000)
def unmake():
canvas.delete(ALL)
root.after(1000)
def loop():
count = 0
while count < 5:
make()
unmake()
count += 1
canvas = Canvas(root,width=100,height=100)
canvas.pack()
loop()
root.mainloop()
Things I've tried:
If I put a print instruction in the make() and unmake() functions these print at 1 second intervals, so I know the .after() method is working.
If I make an infinite loop with the make() function calling unmake() and unmake calling make() again, this does display the flashing rectangle in the main window (root);
def make():
box = canvas.create_rectangle(20,20,60,60,fill="pink")
root.after(1000,unmake)
def unmake():
canvas.delete(ALL)
root.after(1000,make)
If someone knows why Tkinter is behaving this way, I'd be very grateful for guidance. Thanks.
after() does not call any function in the code posted. The following code illustrates the basic idea
from Tkinter import *
root = Tk()
def make():
canvas.create_rectangle(20,20,60,60,fill="pink")
root.after(1000, unmake)
def unmake():
canvas.delete(ALL)
root.after(1000, make)
canvas = Canvas(root,width=100,height=100)
canvas.pack()
make()
root.mainloop()

pygame my picture wont show up on my window

import pygame
import sys
import os
from pygame.locals import *
#Allows for the editing of a window
pygame.init()
#Sets screen size
window = pygame.display.set_mode((800,600),0,32)
#Names the window
pygame.display.set_caption("TEST")
#Types of colors (red,green,blue)
black = (0,0,0)
blue = (0,0,255)
green = (0,255,0)
yellow = (255,255,0)
red = (255,0,0)
purple = (255,0,255)
lightblue = (0,255,255)
white = (255,255,255)
pink = (255,125,125)
mif="climb.PNG"
mif=pygame.image.load(mif).convert()
#Loop
gameLoop = True
while gameLoop:
pygame.display.flip() #must flip the image o the color is visable
window.blit(mif, (0,0))
window.fill(black) #used to fill the creen with the certian color variables
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameLoop=False #Allows the user to exit the loop/game
pygame.quit() #quit the pygame interface
exit(0)
When I select run my yellow window pops up but no image...
I'm new to pygame and python so maybe I just did something wrong?
Is there a specific place that I should have my image saved to?
I'm running on a windows 8 hp ENVY...
I just want to learn what I'm doing wrong so I don't make the error again. (I should also add that I have tried many different pictures and some where not able to be located.)
First fill window with black color window.fill(black),
then put image on it window.blit(mif, (0,0)),
and last send it to your graphic card (and on your monitor) pygame.display.flip()
window.fill(black)
window.blit(mif, (0,0))
pygame.display.flip()
If your image is as big as window then you don't need to use fill(black).
You can keep image in the same folder with script. Or you can create subfolder only for image to have order in folders.
If you call this folder images then you have to add this to filenames images/climb.PNG.
BTW: better use / than \ in path because \n in path can be interpreted as ENTER/new line as in any text.
But the most properly is:
import os
filename = os.path.join('images', 'climb.PNG')

How do I use Tkinter to create a ball everywhere my mouse clicks?

I am trying to create a program that creates a ball every time the mouse clicks, wherever it clicks. I am new to Tkinter and its syntax, but it seems like a pretty useful GUI.
Thanks
Here is the code. This code also tracks whenever a key is pressed, and it prints the syntax for that key.
from Tkinter import *
root = Tk()
def key(event):
pressedkey = repr(event.char)
print "pressed", pressedkey
def callback(event):
canvas.focus_set()
print "clicked at", event.x, event.y
ball = canvas.create_oval(event.x-15, event.y-15, event.x+15, event.y+15, outline='black', fill='gray40', tags=('ball'))
canvas = Canvas(root, width =1224,height=1024,bg='white')
canvas.bind("<Key>", key)
canvas.bind("<Button-1>", callback)
canvas.pack()
root.mainloop()
Here is the Tkinter documentation

multiple tasks on tkinter canvas?

I am trying to update canvas background at the same time running a binding event.
(from code)In do_popup popup menu will be implemented and conti will continuously change the canvas background color. how can i use popup option while canvas is updating continuously.
Sample code:
from Tkinter import *
root = Tk()
def do_popup(event,w2):
print w2 # inplace of print some popupmenu will be implemented
def conti():
idt=1
while idt==1:
w.config(bg="red") # in place of red it will be a random color
w.update_idletasks()
w= Canvas(root, width=600, height=600)
w.grid(row=0, column=0)
line1 = w.create_line(200,200,300,300, width=10, tags="line1", fill="black")
w.tag_bind(line1, "<Button-3>", lambda e, w2="test1" :do_popup(e,w2))
f = Frame(root)
f.grid(row=0, column=1, sticky=N)
f1=Button(f, text='visual', command=lambda :conti())
f1.grid(row=0, column=1,columnspan=1, sticky=W+N+S+E)
mainloop()
will multiprocessing work?
I am using windows 7 32 bit with python 2.7.3
Thanks in advance
When your script enters the mainloop then the events are executed.
To make reoccurring updates I like to do this:
def conti():
try:
w.config(bg="red") # in place of red it will be a random color
finally:
# start conti after 10 milliseconds,
root.after(10, conti)
# could also be 0ms to handle events
root.after(0, conti)
You can see root.mainloop as
while not (root.quit was called):
root.update()
This way wou can do:
root.quit()
and conti automatically stops.
There is no concurrency as with threads in mainloops.
But you can put a mainloop() somewhere when you create an own dialog box and conti will go on.
If you use the modules tkMessageBox(Python2) or tkinter.messagebox(Python3) then you conti should run while the dialog is open.
Does this answer your questions?
PS: do root.protocol("WM_DELETE_WINDOW", root.quit) to make the mainloop end when you close the window.