使用Lambda和元组发送到多个函数:Python,Tkinter [英] Using Lambda and tuples to send to Multiple Functions: Python, Tkinter

查看:151
本文介绍了使用Lambda和元组发送到多个函数:Python,Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  column1 = [
('H','Hydrogen','Atomic#= 1 \ nAtomic Weight = 1.01\
State = Gas \\\
Category = Alkali Metals '),
('Li','Lithium','Atomic#= 3 \ Atomic Weight = 6.94 \ State \\ Solid \ nCategory = Alkali Metals'),
('Na' '钠','原子#= 11 \原子重量= 22.99\\\
State = Soild\\\
Category =碱金属'),
('K','钾','原子#= 19 \ nAtomic重量= 39.10 \ nState =固体\ nCategory =碱金属'),
('Rb','铷','原子#= 37 \原子重量= 85.47 \国家=固体\\ \\ nCategory =碱金属'),
('Cs','Cesium','Atomic#= 55''原子重量= 132.91'国家=固体'\\ nCategory =碱金属'),
('Fr ','Francium','Atomic#= 87\\\
Atomic Weight = 223.00\\\
State = Solid \\\
Category = Alkali Metals')]
#用循环创建所有按钮
r = 1
c = 0
for column1:
tk.Button(self,text = b [0],width = 5,height = 2,bg =gray,command = lambda text = b [1] :self.name(text))。grid(row = r,column = c)
r + = 1
if r> 7:
r = 1
c + = 1

...

  def name(self,text):
self.topLabel.config(text = text)

文字信息(self,text):
self.infoLine.config(text = text)

我想使用这些元组并将第二个位置(元素名称)发送到name()函数(我现在有和正在工作),并将第三个位置(所有元素信息)发送到info()函数,并将它们都打印出来,但它们将位于不同的位置。无论我尝试什么,我似乎都无法做到。你可以使用元组发送多个东西到不同的函数吗?

解决方案

在你创建按钮的行上,你可以完成这个任务(愚蠢的)lambda技巧:

$ $ p $ $ $ $ $ $ $ tk.Button(self,text = b [0],width = 5,height = 2,bg =gray,
command = lambda text = b:[self.name(text [1]),self.info(text [2])]).grid(row = r,column = c)

或定义一个单独的函数来调用它们:

  tk.Button(self,text = b [0],width = 5,height = 2,bg =gray,
command = lambda text = b:self.call_both(text))。grid(row = r,column = c)
$ b $ def call_both(self,line):
self.name(line [1])
self.info(line [2])


column1 = [
('H', 'Hydrogen', 'Atomic # = 1\nAtomic Weight =1.01\nState = Gas\nCategory = Alkali Metals'),
('Li', 'Lithium', 'Atomic # = 3\nAtomic Weight = 6.94\nState = Solid\nCategory = Alkali Metals'),
('Na', 'Sodium', 'Atomic # = 11\nAtomic Weight = 22.99\nState = Soild\nCategory = Alkali Metals'),
('K', 'Potassium', 'Atomic # = 19\nAtomic Weight = 39.10\nState = Solid\nCategory = Alkali Metals'),
('Rb', 'Rubidium', 'Atomic # = 37\nAtomic Weight = 85.47\nState = Solid\nCategory = Alkali Metals'),
('Cs', 'Cesium', 'Atomic # = 55\nAtomic Weight = 132.91\nState = Solid\nCategory = ALkali Metals'),
('Fr', 'Francium', 'Atomic # = 87\nAtomic Weight = 223.00\nState = Solid\nCategory = Alkali Metals')]
#create all buttons with a loop
r = 1
c = 0
for b in column1:
    tk.Button(self,text=b[0],width=5,height=2, bg="grey",command=lambda text=b[1]:self.name(text)).grid(row=r,column=c)
    r += 1
    if r > 7:
        r = 1
        c += 1

...

def name(self, text):
    self.topLabel.config(text=text)

def info(self, text):
    self.infoLine.config(text=text)

I want to use these tuples and send the 2nd position (the element name) to the name() function(which i currently have and works), and the 3rd position (all of the element information) to the info() function, and print them both out, but they will be in different locations. No matter what I try, I cant seem to be able to do so. Can you send multiple things using tuples to different functions?

解决方案

On the line that you create your button, you can accomplish this either with a (stupid) lambda trick:

tk.Button(self,text=b[0],width=5,height=2, bg="grey", 
command=lambda text=b:[self.name(text[1]), self.info(text[2])] ).grid(row=r,column=c)

or define a separate function that calls both:

tk.Button(self,text=b[0],width=5,height=2, bg="grey", 
command=lambda text=b:self.call_both(text)).grid(row=r,column=c)

def call_both(self, line):
    self.name(line[1])
    self.info(line[2])

这篇关于使用Lambda和元组发送到多个函数:Python,Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆