In Tkinter, how can I pack a canvas to the upper left corner and a button to the lower right corner? I tried with can.pack(side=...) and button.pack(side=....) but no luck. I want to get something like this Picture.
You were close. You need to incorporate one more option: anchor.
Below is a simple script to demonstrate:
import Tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(bg="red", height=100, width=100)
canvas.pack(anchor=tk.NW)
button = tk.Button(text="button")
button.pack(side=tk.RIGHT, anchor=tk.SE)
root.mainloop()
When you resize the window, notice how the canvas stays in the upper lefthand corner and the button stays in the lower righthand corner.
Related
I have a question regarding placed buttons on a frame!
I created a canvas and placed a frame inside this canvas! Inside the canvas I place rectangles and oder stuff, drawn by tkinters canvas.create_rectangle, whereas I place certain widgets like buttons inside the frame. (Result: There are togehter in one window)
window_canvas = Canvas(root, borderwidth=0, background="grey", width = window_canvas_size[0], height = window_canvas_size[1], highlightthickness=0)
window_frame = Frame(window_canvas, background='white', borderwidth=0, width = canvas_size[0], height = canvas_size[1])
Now I made the window_canvas scrollable via:
def OnMousewheelConfigureProcessCanvas(self, event):
window_canvas.yview_scroll(int(-1*(event.delta/120)), "units")
window_canvas.bind("<MouseWheel>", m.OnMousewheelConfigureProcessCanvas)
The buttons are scrollable with the window_canvas, when I place the cursor outside of them, no problem.
However, when I hover with my mouse cursor above a button, the "scrollability" just doesn't work anymore. The window_canvas doesn't react to the mouse wheel this time.
I dont want to create a new window (via canvas.create_window) for every button. I have quite a few of them on screen, so I tried to keep it simple by placing one frame for all inside a canvas!
Looking forward to your solutions!
Thanks!
How would I place an image inside of this code so it can scroll?
This code is from the answer here: tkinter: using scrollbars on a canvas
from tkinter import *
root=Tk()
frame=Frame(root,width=300,height=300)
frame.pack(expand=True, fill=BOTH) #.grid(row=0,column=0)
canvas=Canvas(frame,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
hbar=Scrollbar(frame,orient=HORIZONTAL)
hbar.pack(side=BOTTOM,fill=X)
hbar.config(command=canvas.xview)
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(width=300,height=300)
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.pack(side=LEFT,expand=True,fill=BOTH)
root.mainloop()
I tried an image on the canvas, as well as an image inside another frame inside the canvas, and an image inside the existing frame, and I placed a label inside a nested frame and on existing canvas. The scroll bars don't function with the image that was larger than 300x300, but the scroll bars move with the label of only text, but the label doesn't move. I used L_text.place(x=20,y=90) and I used L_text.pack() with same results.
Without modification of code in the Answer, I get scrollbars to move and mousewheel moves them only when mouse pointer hovers on the vert scrollbar, but mouse on the canvas/frame area doesn't move the scrollbars.
Ubuntu 20.04 python 3.8.5
Posting code modified from above sample that is improved for my application.
I need to tweak some more, but this is at least the solution to question in title of post.
import tkinter as tk
from tkinter import *
import PIL
from PIL import Image, ImageTk
root=Tk()
root.geometry("550x850+50+50")
frame=Frame(root,width=300,height=300)
frame.pack(expand=True,fill=BOTH)
canvas=Canvas(frame,width=300,height=300,scrollregion=(0,0,500,1200))# 1200 is
# vertical scrollable area inside the window
hbar=Scrollbar(frame,orient=HORIZONTAL)
hbar.pack(side=BOTTOM,fill=X)
hbar.config(command=canvas.xview)
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(width=300,height=300)
canvas.config(xscrollcommand=hbar.set,yscrollcommand=vbar.set)
canvas.pack(side=LEFT,expand=True,fill=BOTH)
#frame_2=Frame(canvas,width=900,height=900)
#frame_2.pack() # place(x=1,y=1)
file='test_receipt.png'
img=ImageTk.PhotoImage(Image.open(file))
canvas.create_image(1,1,image=img,anchor=NW) #Place image directly on canvas
# 1,1 is upper left corner no padding, anchor required or image isn't alinged
# on its upper left corner.
#L_image=Label(canvas,image=img)
#L_image.pack() # place(x=1,y=1)
#L_text=Label(frame_2,text='label inside')
#L_text.pack()
#L_text.place(x=20,y=90)
root.mainloop()
I try to make a splash screen with a png.
I use Python 3.7.4 64-bit, under macOS 10.14.4 and Visual Code 1.33.1
With root.overrideredirect(True) no windows are displayed.
With root.overrideredirect(False) the png is correctly displayed but the top window border is visible.
import tkinter as tk
root = tk.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)
# Turn off the window shadow
root.wm_attributes('-transparent', True)
# Set the root window background color to a transparent color
root.config(bg='systemTransparent')
root.geometry('+300+300')
# Store the PhotoImage to prevent early garbage collection
root.image = tk.PhotoImage(file='./local/pics/splash.png')
# Display the image on a label
label = tk.Label(root, image=root.image)
# Set the label background color to a transparent color
label.config(bg='systemTransparent')
label.pack()
root.mainloop()
Thanks for your help
This will hide the title bar
and fixates your window to top always:
root.attributes('-type', 'dock')
If you don't need it at the top:
root.attributes('-type', 'splash')
This will hide the title bar
If this can be improved please comment.
I am trying to display an image on my desktop without any boarder or window, like an image floating in the desktop. I also want to be able to control its position once it has been created, with the arrow keys or with a line of code that changes its position with some coordinate system of some sorts. I haven't found any method after some reaserch (not in Python, at least).
If that's not possible, please recommend another programming language that can.
You can use Tkinter for this.
I made a little example:
Python3
from tkinter import Toplevel, Tk, Label, PhotoImage
win = Tk()
win.attributes('-alpha', 0.0)
win.iconify()
window = Toplevel(win)
window.geometry("500x500+100+100")
window.overrideredirect(1)
photo = PhotoImage(file="test.png")
label = Label(window, image=photo)
label.pack()
win.mainloop()
Python2
from Tkinter import Toplevel, Tk, Label
import ImageTk
win = Tk()
win.attributes('-alpha', 0.0)
win.iconify()
window = Toplevel(win)
window.geometry("500x500+100+100") # create an window 500x500 pixel, 100 pixels from the upper left corner
window.overrideredirect(1) # Take the border away
photo = ImageTk.PhotoImage(file="test.png")
label = Label(window, image=photo)
label.pack()
win.mainloop()
Every time I use this code in my applications:
tkMessageBox.showinfo("Test", "Info goes here!")
a message box pops up (like it is supposed to), but after I click OK, the box disappears along with most of the other widgets on the window. How do I prevent the other widgets from disappearing?
Here Is My Code:
from Tkinter import *
import tkMessageBox
root = Tk()
root.minsize(600,600)
root.maxsize(600,600)
p1 = Label(root, bg='blue')
p1.place(width=600, height=600)
b1 = Button(p1, text="Test Button")
b1.place(x="30", y="50")
tkMessageBox.showinfo("Test", Info")
root.mainloop()
Ok, there are a few things going wrong here. First, your label has no string or image associated with it. Therefore, it's width and height will be very small. Because you use pack, the containing widget (the root window) will "shrink to fit" around this widget and any other widgets you pack in the root window.
Second, you use place for the button which means its size will not affect the size of the parent. Not only that, but you place the button inside the very tiny label. Thus, the only thing controlling the size of the parent is the label so the main window ends up being very small.
You have another problem is that you're showing the dialog before entering the event loop. I'm a bit surprised that it even works, but Tkinter sometimes does unusual things under the covers. You should enter the event loop before calling the dialog.
Try this variation of your code as a starting point:
from Tkinter import *
import tkMessageBox
def showInfo():
tkMessageBox.showinfo("Test","Info")
root = Tk()
p1 = Label(root, bg='blue', text="hello")
p1.pack()
b1 = Button(root, text="Test Button", command=showInfo)
b1.pack()
root.mainloop()