I'm fairly new to Python and GUI programming, but while trying to create a simple canvas program I ran into the following problem:
When I use the create_text function(x, y etc.) it creates the text nicely at (x, y), but when I add a font the text shifts to the left and is no longer created at (x, y).
Can somebody tell me how I can fix this, and get the text at (60, 40) (see code)
from tkinter import *
class CanvasShapes:
def __init___(self):
master = Tk()
self.c1 = Canvas(master, width = 200, height = 100)
self.c1.grid(columnspan = 5)
#there are more buttons, but this is the only relevant one for this problem
Button(master, text = "String", command = self.String).grid(row = 1, column = 3)
def String(self):
self.c1.create_text(60, 40, text = "Hi, I am a string", font = "Times 16 bold underline")
def main():
Canvas1 = CanvasShapes()
main()
By default, the x,y represents the center point of the text. If you make the font larger or smaller, the center remains the same but the edges will change depending on the size of the text.
Add anchor='nw' to the create_text command to have the coordinates reoresent the upper-left (northwest) corner of the text.
Related
I'm making a text editor and I'm adding a navbar but the text on FileBtn doesn't appear.
from tkinter import *
##############################################
# Constants
width = 800
height = 450
##############################################
# Window Creation
Window = Tk()
Window.geometry(F'{width}x{height}')
Window.title("TextWrite")
##############################################
# Navbar Creation
Navbar = Frame(Window, width = width, height = 25, bg = "#141414")
Navbar.place(x = 0, y = 0)
# File Options Creation
def OpenFileOptions():
FileOptions.place(x = 0, y = 25)
FileBtn = Button(Window, width = 10, height = 25, text = "File", bg = "#171717", fg = "white", command = OpenFileOptions)
FileBtn.place(x = 0, y = 0)
FileOptions = Frame(Window, width = 10, height = 50)
FileOptions.place(x = -1000, y = 0)
##############################################
# Text Input Creation
Input = Text(Window, width = width, height = 425, bg = "#202020", fg = "white")
Input.place(x = 0, y = 25)
Window.mainloop()
I searched for my problem but nothing I found seemed to fix it.
This is the first time I have encountered this error, and I have no clue why it happens.
The main problem is that you've set the height of the button to 25 lines tall. The width and height for some widgets -- including buttons -- is in number of characters, not pixels. Tkinter will center the text in the widget, so the text was very far down and out of view.
You can actually see this if you remove the text widget. You'll see that the button is very tall and the button label is centered in the button but roughly in the middle of the widget. (note: the following screenshot was taken on a Mac, which doesn't support changing the background color of buttons)
If you remove the height attribute altogether or set height to 1, you'll see the text, though you might have to also change the colors.
If you're just now learning tkinter, I strongly encourage you to use pack and/or grid rather than place. There's a slight learning curve, but it's much easier to make GUIs that are responsive and that make optimal use of the window size. place is best for very unique circumstances rather than as a general purpose tool.
but the text on FileBtn doesn't appear
No need to change value. In line 37, change this y=25 to y=400. Should be like this: Input.place(x = 0, y = 400)
Screenshot:
I’m a first timer at tkinter(python) and what I want to do is to make a line of text stay on the same coords ratio on the canvas. For example, I want a line of text to stay in the middle. Is there any tkinter text parameters that make it stay in a certain ratio without running a while loop? I want minimal time complexity.
Your GUI can have a function bound to the Canvas <Configure> event that fires whenever the Canvas changes size. A simple example below.
There is also a Canvas.scale method which will change the location and size of canvas objects. Text may move but will not change size.
import tkinter as tk
root = tk.Tk()
# Create a canvas that will change size with the root window.
canvas = tk.Canvas( root, width = 600, height = 400, borderwidth = 2,
relief = tk.SOLID )
canvas.grid( sticky = 'nsew', padx = 5, pady = 5 )
root.grid_columnconfigure( 0, weight = 1 )
root.grid_rowconfigure( 0, weight = 1 )
text = canvas.create_text( 50, 50, text = 'Test Text' )
def on_canvas_config( event ):
""" This is bound to the canvas <Configure> event.
It executes when the canvas changes size.
The event is an object that contains the new width and height.
"""
x = max( 25, event.width // 2 ) # x >= 25
y = max( 12, event.height // 8 ) # y >= 12
canvas.coords( text, x, y ) # Set the canvas coordinates
canvas.bind( '<Configure>', on_canvas_config )
root.mainloop()
The on_canvas_configure function can be written to amend the coords of any objects in the Canvas to meet your requirements. I've never tried to use the Canvas.scale method but this may be worth exploring if there are many objects to reposition on the canvas.
For my tkinter app I want to make a frame that would on top of other widgets, not taking any space (like html position fixed).
The Frame will have to contain widgets, if Frame is not possible labels or buttons will do.
I have no idea how to do it so haven't tried anything yet. Please help!
Here is a demonstration of place manager.
Remarks in the code explain behaviour.
import tkinter as tk
root = tk.Tk()
root.geometry("868x131")
button = tk.Button(root, text = "A Button")
button.place(x = 2, y = 2)
Frame = tk.LabelFrame(root, text = "A LabelFrame using place", bg="cyan")
# Frame using place with x, y, width, height in absolute coordinates
Frame.place(x = 250, y = 20, width = 600, height = 70)
ButtonInFrame = tk.Button(Frame, text = "A Button IN LabelFrame", bg="white")
# Note: place x,y is referenced to the container (Frame)
# Note: place uses just x,y and allows Button to determine width and height
ButtonInFrame.place(x = 2, y = 2)
Label = tk.Label(root, text = "A Label on LabelFrame\nWith multiple lines\nOf Text.", bg="light green")
# Label sits on top of buttonFrame and Frame
# Note place uses just x,y and allows Label to determine width and height
Label.place(x = 330, y = 60)
root.mainloop()
I'm working on a function in a UI class that is a config window, it displays the logo of the program and has an updating text at the bottom telling you what it's loading, etc. This is what I have so far:
self.window = "config"
self.windowWidth = 340
self.windowHeight = 270
infoText = "Configuring Kh..."
self.root = tk.Tk()
self.root.geometry("%dx%d+400+400" % (self.windowWidth, self.windowHeight))
self.root.title("Kh Control v1.1 starting...")
logo = tk.PhotoImage(file="KhLogo.gif")
mainPanel = tk.Canvas(self.root, width=self.windowWidth, height=self.windowHeight)
mainPanel.image = logo
mainPanel.pack()
mainPanel.create_image(0, 0, image=logo, anchor="nw")
mainPanel.create_text(0,200, text=infoText, anchor="nw", fill="yellow")
return
I'd like the text in infoText to be centered horizontally and offset vertically about 200px down. The vertical offset works fine, but I can't figure out how to center the text horizontally.
I started by trying the age old ((width / 2) - (str length / 2)) but then realized that each letter isn't 1px. And anchor = "center" seems to only put half the text off the left side of the screen.
I'm very new to Python (only a few days now) so if I'm missing something obvious, that's why.
EDIT: and in case it wasn't obvious, this text will change so I can't just make an absolute decision on the offsets, it has to change with the text
I figured it out after scrounging through the canvas reference.
There is a method for a canvas called bbox that returns a tuple containing (x1, y1, x2, y2) of the area an item takes up. I got those coords, drew up a function to find the px length of it, divided it by 2 and subtracted it from the window width. Then I used canvas.move to change the x offset using the number the function returned.
def findXCenter(self, canvas, item):
coords = canvas.bbox(item)
xOffset = (self.windowWidth / 2) - ((coords[2] - coords[0]) / 2)
return xOffset
The main part is here:
textID = mainPanel.create_text(0,0, text=infoText, anchor="nw", fill="yellow")
xOffset = self.findXCenter(mainPanel, textID)
mainPanel.move(textID, xOffset, 0)
Hopefully my hours of searching for this answer will help someone later on.
Have you tried using the justify parameter?
Often center is used with non-text objects.
https://stackoverflow.com/a/15016161/3900967 might provide some insight.
You can set the position of the text using the first part of the .create_text() method.
see http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/create_text.html
To make this position horizontialy center to the window use self.windowWidth / 2 as the x cordinate
By default the text is anchored to center around the given position.
(.create_text will default to anchor="CENTER")
You will also want to remove the anchor="nw" as this will make your text display down and to the right of the position given
As such your updated code should be.
self.window = "config"
self.windowWidth = 340
self.windowHeight = 270
infoText = "Configuring Kh..."
self.root = tk.Tk()
self.root.geometry("%dx%d+400+400" % (self.windowWidth, self.windowHeight))
self.root.title("Kh Control v1.1 starting...")
logo = tk.PhotoImage(file="KhLogo.gif")
mainPanel = tk.Canvas(self.root, width=self.windowWidth, height=self.windowHeight)
mainPanel.image = logo
mainPanel.pack()
mainPanel.create_image(0, 0, image=logo, anchor="nw")
mainPanel.create_text(self.windowWidth/2,200, text=infoText, fill="yellow")
return
You can use the anchor="center" method to align your text that created inside the canvas using canvas.create_text method
try following example code:
from tkinter import *
root = Tk()
canvas = Canvas(root, width=400, height=300)
canvas.pack()
text = canvas.create_text(200, 150, text="Hello World!", anchor="center")
root.mainloop()
I am writing a simple program that pulls up an image (BackgroundFinal.png) and displays it in a window. I want to be able to press a button on the window to move the picture down by 22 pixels. Everything works except the button does not do anything.
import Tkinter
import Image, ImageTk
from Tkinter import Button
a = 0 #sets inital global 'a' and 'b' values
b = 0
def movedown(): #changes global 'b' value (adding 22)
globals()[b] = 22
return
def window(): #creates a window
window = Tkinter.Tk();
window.geometry('704x528+100+100');
image = Image.open('BackgroundFinal.png'); #gets image (also changes image size)
image = image.resize((704, 528));
imageFinal = ImageTk.PhotoImage(image);
label = Tkinter.Label(window, image = imageFinal); #creates label for image on window
label.pack();
label.place(x = a, y = b); #sets location of label/image using variables 'a' and 'b'
buttonup = Button(window, text = 'down', width = 5, command = movedown()); #creates button which is runs movedown()
buttonup.pack(side='bottom', padx = 5, pady = 5);
window.mainloop();
window()
If I am not mistaken, the button should change the global 'b' value, therefore changing the y position of the label. I really appreciate any help, sorry for my god-awful conventions. Thanks in advance!
You have a few problems here.
First, you're using pack and place. In general, you should only use 1 geometry manager within a container widget. I don't recommend using place. That's just too much work that you need to manage.
Second, you're calling the callback movedown when you construct your button. That's not what you want to do -- You want to pass the function, not the result of the function:
buttonup = Button(window, text = 'down', width = 5, command = movedown)
Third, globals returns a dictionary of the current namespace -- It's not likely to have an integer key in it. To get the reference to the object referenced by b, you'd need globals()["b"]. Even if it did, changing the value of b in the global namespace won't change the position of your label because the label has no way of knowing that change. And in general, if you need to use globals, you probably need to rethink your design.
Here's a simple example of how I would do it...
import Tkinter as tk
def window(root):
buf_frame = tk.Frame(root,height=0)
buf_frame.pack(side='top')
label = tk.Label(root,text="Hello World")
label.pack(side='top')
def movedown():
buf_frame.config(height=buf_frame['height']+22)
button = tk.Button(root,text='Push',command=movedown)
button.pack(side='top')
root = tk.Tk()
window(root)
root.mainloop()
Thanks for the reply but, It was not really what I was looking for. I'll post what I found worked best here for anybody else with the same problem.
Essentially, It is much better, in this case, to use a Canvas instead of a label. With canvases, you can move objects with canvas.move, here is a simple example program
# Python 2
from Tkinter import *
# For Python 3 use:
#from tkinter import *
root = Tk()
root.geometry('500x500+100+100')
image1 = PhotoImage(file = 'Image.gif')
canvas = Canvas(root, width = 500, height = 400, bg = 'white')
canvas.pack()
imageFinal = canvas.create_image(300, 300, image = image1)
def move():
canvas.move(imageFinal, 0, 22)
canvas.update()
button = Button(text = 'move', height = 3, width = 10, command = move)
button.pack(side = 'bottom', padx = 5, pady = 5)
root.mainloop()
my code may not be perfect (sorry!) but that is the basic idea. Hope I help anybody else with this problem