如何从tkinter获取条目输入到python脚本?

2024-10-02 02:39:21 发布

您现在位置:Python中文网/ 问答频道 /正文

几周前我开始用Python编程。我已经做了一个python程序,因此我现在用tkinter制作一个gui。gui中的用户输入将由我已经编写的python程序处理。但是当我运行我的程序并想在python程序中使用一个条目字段的值时,它不会打印条目的输入值。而是打印0,0。在

我得到了以下代码: 我有一个类,我用tkinter(gui)为我的gui制作了一个框架_泵.py)公司名称:

import GUI as Gui
from GUI import *
import pump as Pomp

class Pump1(tk.Frame):
    p1_pump_spd1 = 0
    d_p1_mtr_on = 0

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text='Test case')
        label.place(x=380, y=10)

        def validate(new_text):
            if not new_text:  # the field is being cleared
                self.entered_number = 0
                return True

            try:
                self.entered_number = int(new_text)
                return True
            except ValueError:
                return False

        def ok():
            global p1_pump_spd1
            p1_pump_spd1 = p1.get()
            try:
                p1_pump_spd1
            except ValueError:
                p1_pump_spd1 = 0
            print(Pomp.p1_pump_spd)

        vcmd = parent.register(validate)
        p1 = tk.Entry(self, width=10, validate='key', validatecommand=(vcmd, '%P'))
        btn_ok = tk.Button(self, text='ok', command=ok, width=2)
        p1.place(x=230, y=20)
        btn_ok.place(x=290, y=20)

我得到了python程序,我想在那里处理输入值(泵.py)在

^{pr2}$

不管p1,pump,I都会输入,但是如果pump输入的值是0,那么输入值都是0。任何帮助都将不胜感激。在


Tags: textimportself程序newdefplacegui

热门问题