[英]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. 顺便说一句:之后,您可以从文件中读取points
和connections
。
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模块可用于具有nodes
和edges
( points
和connections
)的graphs
,但是它对您来说并不是那么有用。 See: random draw . 请参阅: 随机抽奖 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.