简体   繁体   English

如何提取一个函数的参数并将其用于另一个使用与第一个函数有2个不同结果的函数中?

[英]How to extract parameters of a function and use them in another function that uses 2 different results from the 1st function?

New to coding, not sure how to ask this. 编码新手,不确定如何问这个。 I have a function that creates buttons: 我有一个创建按钮的功能:

from tkinter import *
root = Tk()
canvas = Canvas(root)

def create_button(location_name, position_x, position_y):
    Button(root, text = location_name).place(x=position_x, y=position_y, width=60, height=20)

a = create_button("A",20,30)
b = create_button("B",70,80)

canvas.grid()
root.mainloop()

I want to create a function that makes a line between any button. 我想创建一个在任何按钮之间划一条线的函数。 For example with A and B it has to take into account the parameters of A (position_x, position_y) and parameters of B (position_x, position_y). 例如,对于A和B,必须考虑A的参数(position_x,position_y)和B的参数(position_x,position_y)。 Is it possible to do that ? 有可能这样做吗?

You should keep this data as list or dictionary and then you can use them with many functions 您应该将此数据保留为列表或字典,然后可以将其用于许多功能

# --- functions ---

def create_button(name):
    x, y = points[name]

    button = Button(root, text=name)
    button.place(x=x, y=y, width=60, height=20)

    return button

def create_line(name1, name2):
    x1, y1 = points[name1]
    x2, y2 = points[name2]

    line = canvas.create_line(x1, y1, x2, y2, ...)

    return line

# --- main ---

points = {
    "A": [20,30], 
    "B": [70,80],
}

buttons = {}
lines = {}

buttons["A"] = create_button("A")
buttons["B"] = create_button("B")
lines[("A", "B")] = create_line("A", "B")

You can even use for loop to create it 您甚至可以使用for循环来创建它

points = {
    "A": [20,30], 
    "B": [70,80],
    "C": [50,0]
}

connections = [
    ("A", "B"), # or "AB" but it needs modification in `for`
    ("A", "C"),
    #("B", "C") # you can skip some connections
]

buttons = {}
lines = {}

for name in points.keys():
    buttons[name] = create_button(name)

for name1, name2 in connections:
    lines[(name1, name2)] = create_line(name1, name2)
    #lines[(name2, name1)] = lines[(name1, name2)] # "AB" = "BA"

BTW: and later you can read points and connections from file. 顺便说一句:之后,您可以从文件中读取pointsconnections


BTW: there is module Networx to work with graphs which have nodes and edges ( points and connections ) but it can be not so usefull for you. 顺便说一句:Networx模块用于具有nodesedgespointsconnections )的graphs ,但是它对您来说并不是那么有用。 See: random draw . 请参阅: 随机抽奖

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个函数返回两个值,然后将它们都用作另一个函数的参数中的两个参数 - How to return two values from a function and then use both of them as parameters within the parameter of another function as two parameters 如何跳过初始化函数的第一个返回值? -Python - How to skip initializing the 1st returned value from a function? - Python 运行一个函数,然后根据第一个函数的结果运行另一个函数 - Running a function and then running another function depending on the result of the 1st one 如何在两个 arguments function 中仅打印第一个参数? - how to print only 1st argument in two arguments function? 如果第一次递归中的语句,递归函数未运行 - Recursive function not running if statements in 1st recursion 使用函数作为参数的函数 - Function that uses functions as parameters 使用在函数中声明的变量,并在不同类的另一个函数中使用它们 - Using variables declared in functions and use them in another function in a different class 如何在不同函数中使用函数中的变量 - How to use variables from a function in a different function 如何在不同的代码块中使用 function 之外的 function 中的参数/变量 - How to use parameters/variables in a function outside of the function in a different block of code 从1st ..开始使用不同结果对记录进行排名 - Ranking records from 1st .. onwards using different results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM