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

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

Related

Why does the text on my button not appear?

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:

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.

tkinter after function doesn't work for me

so i'm very new to coding, i took a tkinter draw program off the internet and wanted to add a change colour function by opening up a new window, having the user input the hex code, then having the window close. However the window isn't even opening up when i add the after function, and just waits 5 seconds showing the button being depressed.
from tkinter import *
root = Tk()
# Create Title
root.title( "Paint App ")
# specify size
root.geometry("500x350")
#set colour
Colour = "#000000"
def changecolour():
col=Tk()
Label(col, text= "Enter the new hex code, you have 5 seconds ").grid(column=1,row=0)
newcol=Entry(col,width=15)
newcol.grid(column=1,row=1)
col.after(5000)
Colour=newcol.get()
col.destroy()
# define function when
# mouse double click is enabled
def paint( event ):
# Co-ordinates.
x1, y1, x2, y2 = ( event.x - 5 ),( event.y - 5 ), ( event.x + 5 ),( event.y + 5 )
# specify type of display
w.create_oval( x1, y1, x2,y2, fill = Colour )
# create canvas widget.
w = Canvas(root, width = 400, height = 250)
# call function when double
# click is enabled.
w.bind( "<B1-Motion>", paint )
# create label.
l = Label( root, text = "Click and Drag slowly to draw." )
l.pack()
w.pack()
btn = Button(root, text = 'Change colour', bd = '5', command = changecolour)
btn.place(x= 150,y=300)
mainloop()
Use the built-in colorchooser;
You can try it by running the following command:
python -m tkinter.colorchooser
Use it in your program like this:
from tkinter import colorchooser
def changecolour():
global Colour
_, Colour = colorchooser.askcolor(title ="Choose color")
The askcolor function returns two values;
An RGB tuple
A hex string starting with a #.
Since you only need the latter, I used _, Colour.
Also, seeing as you want to change the global variable Colour, you have to mark it as such in changecolour.

Tkinter -- how to horizontally center canvas text?

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

Change position of label in Tkinter window

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

Categories