python - 将命令分配给按钮 - Tkinter

标签 python tkinter

我想找出必须将哪个命令分配给 Tkinter GUI 中的按钮才能打印结果。

设置是运行 aaaa.py。

def question():
    import xxxx
    return xxxx.hoeveel()


if __name__ == '__main__':
    from bbbb import response
    def juist():
        return response()
    print juist()

运行 aaaa.py 时,我得到一个基于脚本 xxxx.py 的 Tkinter GUI

from Tkinter import *
import ttk



def hoeveel():
    return int(x=gewicht.get(), base=10)

frame = Tk()

gewicht = StringVar()

a = Label(frame, text="Wat is uw gewicht in kilogram?:").grid(row=1, column=1, sticky='w')
aa = Entry(frame, text="value", textvariable=gewicht, justify='center', width=10)
aa.grid(row=1, column=2, padx=15)

bereken = ttk.Button(frame, text='Bereken')
bereken.grid(column=1, row=2, columnspan=2, ipadx=15, pady=25)
mainloop()

Tkinter GUI xxxx.py 中给出的输入被发送到 bbbb.py 进行一些计算。

from aaaa import question
mass_stone = question() * 2.2 / 14


def response():
    return str("Uw gewicht in kilograms is gelijk aan " + ("{0:.5}".format(mass_stone)) + " stone.")

我的问题是,当我关闭 Tkinter 窗口时,我只得到输出“Uw gewicht in kgs is gelijk aan "x (depending on the value input)"stone。

我想在按下按钮时得到结果。

有什么建议吗?

最佳答案

我想我已经找到了答案,但我不确定这是最优雅的方法。 如果您知道其他方式和更正确的方式,请告诉我们。

所以,你需要运行aaaa.py

import xxxx
def question():
    return xxxx.hoeveel()

xxxx.py

from Tkinter import *
import ttk
import bbbb

def hoeveel():
    return int(x=gewicht.get(), base=10)

frame = Tk()

gewicht = StringVar()

a = Label(frame, text="Wat is uw gewicht in kilogram?:").grid(row=1, column=1, sticky='w')
aa = Entry(frame, text="value", textvariable=gewicht, justify='center', width=10)
aa.grid(row=1, column=2, padx=15)

bereken = ttk.Button(frame, text='Bereken', command=bbbb.berekening)
bereken.grid(column=1, row=2, columnspan=2, ipadx=15, pady=25)
mainloop()

最后

bbbb.py

from aaaa import question
def berekening():
    mass_stone = question() * 2.2 / 14
    print mass_stone

当你运行 aaaa.py 时,你会得到带有问题的 Tkinter Gui。 填写您的体重,然后按“Bereken” 你会得到我想要的打印出来的答案。

关于python - 将命令分配给按钮 - Tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38590087/

相关文章:

python - 文件对象(f = open(filename))使用一次后就过期了?

python - 使用另一个分类变量 'share' 绘制 'Day' 列的箱线图

python - 如何评估 scikit 学习 LogisticRegression 的成本函数?

Python Tkinter - 关闭对话框而不关闭主窗口

python - 为什么此 Python 代码中出现缩进错误

Python tkinter Entry() 不向变量返回任何值

python - 当我使用requirements.txt时,pip没有从virtualenv中安装到site-packages目录

python - 使用 GMAIL API 阅读最近 20 条消息

python - 与预测连接时的数据

python - 如何在Python中停止request.get