Reputation: 709
Hey guys I am trying to display text on the screen using tkinter and using a variable to specify where it is but I keep getting this error:
_tkinter.TclError: bad geometry specifier.
Here is the code:
from tkinter import*
root = Tk()
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
dsw = sw / 2
dsh = sh / 2
print(dsw)
print(dsh)
label = Label(text='text', font=('Arial','30'), fg='black', bg='white')
label.master.overrideredirect(True)
label.master.geometry("+dsw+dsh")
label.master.lift()
label.master.wm_attributes("-topmost", True)
label.master.wm_attributes("-disabled", True)
label.master.wm_attributes("-transparentcolor", "white")
label.pack()
label.mainloop()
It seems like the problem is with the label.master.geometry line. How do I format the geometry data properly?
Upvotes: 0
Views: 517
Reputation: 3116
dsw = sw / 2
to dsw = str(int(sw / 2))
so that is takes the string representation of integer value of (sw/2).geometry("+dsw+dsh")
to geometry("+" + dsw + "+" + dsh)
. Because ("+dsw+dsh")
represents ("+dsw+dsh") whereas ("+" + dsw + "+" + dsh)
represents something like this: (+683+384)Below is updated Code:
from tkinter import*
root = Tk()
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
dsw = str(int(sw / 2))
dsh = str(int(sh / 2))
print(dsw)
print(dsh)
label = Label(text='text', font=('Arial','30'), fg='black', bg='white')
label.master.overrideredirect(True)
label.master.geometry("+"+dsw+"+"+dsh)
label.master.lift()
label.master.wm_attributes("-topmost", True)
label.master.wm_attributes("-disabled", True)
label.master.wm_attributes("-transparentcolor", "white")
label.pack()
label.mainloop()
Upvotes: 1
Reputation: 439
The .geometry()
method takes a string 'widthxheight'
Change the line label.master.geometry("+dsw+dsh")
to this and it should work:
label.master.geometry(str(dsw) + 'x' + str(dsh))
I am not sure though that it would do what you want it to do. To put the text on screen where you want it to you should use your variables dsw and dsh to set the correct padding for the pack() method. Geometry is used to change the size of tkinter window. Read more about this here: http://effbot.org/tkinterbook/wm.htm
Upvotes: 1