【问题标题】:Change variable value according to functions results根据函数结果更改变量值
【发布时间】:2020-11-06 10:21:00
【问题描述】:

大家好!

我创建了一个 tkinter 界面,其中 3 个按钮在 cliqued 后启动 3 个不同的功能。每个函数都从不同位置(COM 端口和 Selenium/webdriver)重新获取数据。

通过这个不同的函数,我想更新我打算稍后打印的变量,但是当我尝试在每个函数的末尾使用“return”时,这不起作用。

因此,我认为我的结构是错误的,但我不明白如何,请您帮忙理解这里的问题吗?

import tkinter as tk

stack_lenght = 0
frequency_P = 0
frequency_S1 = 0
frequency_S2 = 0
stack_power = 0
amplitude1 = 0
amplitude2 = 0
amplitude3 = 0
amplitude4 = 0


def Get_lenght():
   #my code 
   return stack_lenght = lenght_measure

def Get_frequencies():
   #my code 
   return frequency_P = Frequency_measured

def Get_amplitudes():
   #my code 
   ....

# My Tkinter interface bellow

谢谢!

【问题讨论】:

  • 您可能想使用global,因为在函数内部所做的更改是在本地范围内,不会在函数外部生效。

标签: python function variables tkinter


【解决方案1】:

你可以试试:

def Get_lenght():
    #your code 
    global stack_lenght
    stack_lenght = lenght_measure
    return stack_lenght

#rest of code

对我来说,运行 python 3.8.3,尝试在返回时分配值时会抛出错误语法。

将值定义为global stack_lenght 将保留在Get_lenght() 函数中设置的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-06
    • 2016-07-11
    • 2018-10-30
    • 2011-10-29
    • 2020-01-02
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多