Tkinter -- how to horizontally center canvas text? - python

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

Related

How do I create a function that will create and place a button, with tkinter, based in input values in the argument?

I'm trying make a python function that will take in arguments, and create a button from tkinter.
Some background, this is for a larger project with a lot of buttons being used, and the purpose of this is to increase efficiency when adding new button. I also wanted a central place where I can change a small variable to switch around every button at once instead of having to adjust every single button command.
So pieces that I am using in the larger project to create the tkinter window are being used, which includes some add ball bits and pieces.
That main way I have chosen to place buttons location is based on x, y points, I know there are easier ways, however this is just have I have chosen to do it.
The current problem is what then I run the code, the button is created, however the button is not responsive and the function the button would run when pressed, runs(which shouldn't be happening). As in the command doesn't run, but the button shows its being pressed when clicked on the tkinter window.
from tkinter import *
root = Tk()
winWidth = 500
winHeight = 500
HEIGHT = 30
WIDTH = 100
SIZE = 15
PHOTO = PhotoImage(
file=r"C:\\Users\\Paint\\VS Code\\1x1 pixel.png", width=1, height=1)
screeenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
cenX = int(screeenWidth/2 - winWidth/2)
cenY = int(screenHeight/2 - winHeight/2)
root.geometry(f'{winWidth}x{winHeight}+{cenX}+{cenY}')
root.title('Button Testing')
def Create_Button(BUTTON : str, Text : str, Command, Width : int, Height : int, x : int, y: int):
BUTTON = Button(
root,
text = Text,
command = Command,
width = WIDTH + Width,
height = HEIGHT + Height,
font = 'BOLD, 15',
compound = 'center',
image = PHOTO
)
BUTTON.place(
x=(winWidth/2 - (BUTTON['width'])/2)+int(x),
y=(winHeight/2 - (BUTTON['height'])/2-int(y)))
def Hi():
print('hellow')
Create_Button('TheBUtton','Test', Hi() , 0, 0, 0, 0)
root.mainloop()
I thought the issue might be from the button function being full of variables, so that when it is pressed on the tkinter window, it can't recall what it needs to do. However I have no clue if this is how or why this is present. (let me know if this is the case, and if so how I would work around it or if I should try another thing)
All my different attempts ended with the same result as when I run the script, the button creation function runs, creating and placing a button, but also running the function what would run when the button is pushed (which shouldn't be happening normally), and then getting an unresponsive button on the tkinter window.
My expectation is for the Create_Button function to create and place a working button based on the arguments in the Create_Button function.
If there are other suggestions on how to reach the same result in a different method, I would love to hear it.
from tkinter import *
root = Tk()
winWidth = 500
winHeight = 500
HEIGHT = 30
WIDTH = 100
SIZE = 15
screeenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
cenX = int(screeenWidth/2 - winWidth/2)
cenY = int(screenHeight/2 - winHeight/2)
root.geometry(f'{winWidth}x{winHeight}+{cenX}+{cenY}')
root.title('Button Testing')
def Create_Button(BUTTON : str, Text : str, Command, Width : int, Height : int, x : int, y: int):
BUTTON = Button(
root,
text = Text,
command = Command,
font = 'BOLD, 15',
compound = 'center'
)
BUTTON.place(
x=(winWidth/2 - (BUTTON['width'])/2)+int(x),
y=(winHeight/2 - (BUTTON['height'])/2-int(y)),
width = WIDTH + Width,
height = HEIGHT + Height)
return BUTTON
def Hi():
print('hellow')
btn_1 = Create_Button('TheBUtton','Test', Hi() , 10, 10, 10, 10)
btn_2 = Create_Button('TheBOOOOOUtton','not a test?', Hi() , 10, 10, 10, 100)
btn_1["text"] = "new text"
root.mainloop()
i edited your code so it works now
okay first in your code i would recommend you put width and height in the .place since when you put it in the button it accounts not for the pixels but for the len and rows of the text
ex
'''this
here'''
it's width is 4 and height is 2 (4 alapbets across and 2 rows)
and also your program im not understanding alot like the pixels of the root and
Espicially your code isnt working cause the button is being huge since your adding 500 and it is inside the button not the .place
since width = WIDTH + Width, WIDTH=100 Width=0 that makes it 500 alaphlabets
and it is 30 for the height(rows)
i also added a return func which makes u able to edit it in the future
and maybe doing anchor=tk.CENTER in the .place will make it better imo but i didnt add that in the code

Is there a Python tkinter function that makes a drawing’s coords on a certain ratio on the canvas widget?

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.

Cannot use .place for a label frame in tkinter

I've been working on adding label frames to my window but for some reason whenever I use .place it never places the frame. Grid and pack work though. I'm trying to get the label frame right in the middle of the screen through coordinates. Heres my code (the error is somewhere in the createstock functiom):
import tkinter as tk
import yfinance
class StockWindow:
def __init__(self,master,number):
self.master = master
self.frame=tk.Frame(self.master)
self.frame.pack()
w, h = master.winfo_screenwidth(), master.winfo_screenheight()
self.master.overrideredirect(1)
self.master.geometry("%dx%d+0+0" % (w, h))
self.master.focus_set() # <-- move focus to this widget
self.master.bind("<Escape>", lambda e: e.widget.quit())
#################################################
## Labels
self.amountChanged = tk.Label(self.master,text = "$1000")
self.amountChanged.place(x=w/2,y=h/2)
self.highestChangedStock = tk.Label(self.master,text = "Amzn")
self.highestChangedStock.place(x=w/2+10,y=h/2+40)
self.lowestChangedStock = tk.Label(self.master,text = "this one")
self.stockTips = tk.Label(self.master,text = "Buy some")
self.stockTips.place(x=2,y=777)
self.marketChange = tk.Label(self.master,text = "Alot!")
self.marketChange.place(x=23,y=66)
self.stockNews = tk.Label(self.master,text = "News Here!")
self.stockNews.place(x=23,y=234)
self.stockNewds = tk.Label(self.master,text = "News Hewewere!")
self.stockNewds.place(x=300,y=300)
## Buttons
self.seeAllStocks = tk.Button(self.master,text ="do you wanna see more stocks?")
self.seeAllStocks.place(x=0,y=0)
self.goBack =tk.Button(self.master,text = "Go back",command=self.close_windows)
self.goBack.place(x=100,y=100)
self.createStock("afdhsfdhsfhsfghsgfhsdg",3,30)
#########
def createStock(self,name,pricechange,placement):
stockframe = tk.LabelFrame(self.frame,text='')
#stockframe.size(200)
stockframe.place(x=400,y=400,height=10,width=10)
#stockframe.pack(expand='yes',fill='both')
#stockframe.grid(column=5,row=5)
tempLabel = tk.Label(stockframe,text=name)
tempLabel.pack()
def close_windows(self):
self.master.destroy()
The problem is that self.frame is used as the parent of the label frame, but it has a height of 1x1 since there's nothing in it. Thus, any widget placed inside it will also be invisible.
If you want to use place to center a widget, the simplest solution is to use relative coordinates with a relative x and y coordinate of .5 (ie: 50% of the width and height of the window).
For example, you don't need to place the labelframe inside a label inside the root window. Just remove self.frame and use relative coordinates for the label frame:
stockframe = tk.LabelFrame(self.master,text='')
stockframe.place(relx=.5, rely=.5)

Canvas resizing with window - good way to do it?

i created a window with a canvas inside. The canvas contains an rectangle. Both change their size through a callback together with the window size.
So my beginner question is: It works fine, but is this a good or common way to do this? Or is there a more efficient/common way?
from tkinter import* #
#creating instance of tkinter
obj = Tk()
#Set title of our window form
obj.title("MyFirst Window - WOW")
#Set dimension of form
x_size = 1200
y_size = 600
obj.geometry(str(x_size)+"x"+str(y_size))
obj.update()
w = Canvas(obj, width=x_size, height=y_size)
w.place(x=0,y=obj.winfo_height()-100)
w.create_rectangle(0, 0, obj.winfo_width(), 100, fill="#476042")
def callback(event):
print(str(obj.winfo_width())+'x'+str(obj.winfo_height()))
w.config(width=obj.winfo_width(),height=obj.winfo_height())
w.place(x=0,y=obj.winfo_height()-100)
w.create_rectangle(0, 0, obj.winfo_width(), 100, fill="#476042")
window = obj
window.bind("<Configure>", callback)
obj.mainloop()
No, this is not a good way to have the canvas resize. You should almost never use place. grid and pack make it much easier to create widgets that automatically resize.
For example, if you want the canvas to always be 100 pixels tall and fill the full width of the window, you can add it to obj like this:
w = Canvas(obj, width=x_size, height=100)
w.pack(side="bottom", fill="x")
As for the green rectangle, you have no choice but to use a binding on <Configure> if you want the rectangle to grow and shrink along with the canvas.
However, your callback creates a new rectangle every time it's called instead of modifying the coordinates of the existing rectangle. This is a memory leak, because your program will use more and more memory the longer it runs and the more often the window is resized. w.create_rectangle will return an identifier; you can use that identifier to later modify the rectangle.
Here's a simplified version of your code. I've changed the variable names to make it a bit easier to comprehend.
from tkinter import *
window = Tk()
window.title("MyFirst Window - WOW")
x_size = 1200
y_size = 600
window.geometry(str(x_size)+"x"+str(y_size))
window.update_idletasks()
canvas = Canvas(window, width=x_size, height=100, background="bisque")
canvas.pack(side="bottom", fill="x")
rect = canvas.create_rectangle(0, 0, window.winfo_width(), 100, fill="#476042")
def callback(event):
canvas.coords(rect, 0, 0, canvas.winfo_width(), 100)
canvas.bind("<Configure>", callback)
window.mainloop()

Python canvas text position changes with font

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.

Categories