Home > Back-end >  Using a function to give values for parameters in tkinter
Using a function to give values for parameters in tkinter

Time:05-28

Can you use a function, define them as values and then use it in parameters. Example:

import tkinter as tk

def design_standard()

    height = 25
    width = 25

    return width, height

root = tk.TK
root.title("Test")

test.label(root, text ="Test", design_standard())


and what would happen is that the label "Test" would have width = 25 and height = 25.

CodePudding user response:

Sort of. The key is to understand that calling a function isn't anything special. The function returns something, and calling the function is the same as writing that something. So, what you wrote is the same as writing:

test.label(root, text="Test", (25,25))

And for a Tk label, that won't work. You CAN do it by returning a dictionary, although I'm not sure it's really worthwhile:

def design_standard():

    height = 25
    width = 25

    return {'width':width, 'height':height}

root = tk.Tk()
root.title("Test")

test = tk.Label(root, text ="Test", **design_standard())

The ** operator here "converts" a dictionary into named parameters.

  • Related