【发布时间】:2021-03-23 23:38:33
【问题描述】:
我需要使用 tkinter 为 Raspberry Pi 编写 GUI(触摸屏)。我有两个条目小部件,我需要单独输入。不幸的是,当我在小键盘上输入一个值时,这两个条目都被写入了。如何更改它,我只能在已选择的条目小部件中输入一个值?我认为问题出在新闻功能和表达的部分..?而且我的退出按钮也不会关闭我的窗口...?
感谢您的帮助!
这是我的代码:
import sys
import time
import struct
import select
import signal
import subprocess
import os
import shared
from tkinter import *
import tkinter
import time
ug = 0 #lower bound [cm]
og = 41 #Nutzbare höhe Wassertank [cm]
run = 0
run1 = 1
xdis = 1
ydis = 1
# globally declare the expression variable
expression = ""
# Function to update expression in the text entry box
def press(num):
global expression
expression = expression + str(num)
equation.set(expression)
def clear():
global expression
expression = ""
equation.set("")
#create GUI
if __name__ == "__main__":
# create a GUI window
gui = Tk()
gui.title("GUI")
equation = StringVar()
#Label 1
label1 = tkinter.Label(text ="Pumpenhöhe 1")
label1.grid (row =0 , column =0 , padx = xdis , pady = ydis )
#Eingabefeld 1 definieren
eingabe1 = tkinter.Entry(gui, textvariable=equation, width=4, bg ='#ffffff')
eingabe1.grid(row=0, column=1, padx=xdis, pady = ydis)
eingabe1.focus_set()
#Label 2
label2 = tkinter.Label (text ="Pumpenhöhe 2")
label2.grid(row=1,column =0 , padx = xdis ,pady = ydis)
#Eingabefeld 2
eingabe2 = tkinter.Entry(gui, textvariable=equation, width=4, bg ='#ffffff')
eingabe2.grid(row=1, column=1, padx=xdis, pady = ydis)
#button obj to start thread
start_thread = tkinter.Button(text ="start thread(main loop)", command=start_thread)
start_thread.grid(row=2, column=1, padx=xdis, pady = ydis)
#button obj on framework to start thread
set_setpoints = tkinter.Button(text ="Send", command = set_setpoints)
set_setpoints.grid(row=2, column=2, padx= xdis, pady = ydis)
#create exit button
ex_bt = tkinter.Button(gui, text='Exit', command=gui.quit)
ex_bt.grid(row=7, column=2, sticky=tkinter.W, padx=xdis, pady=ydis)
#buttons for numpad
.
.
.
clear = Button(gui, text='Clear',
command=clear, height=1, width=7)
clear.grid(row=6, column='1')
Decimal= Button(gui, text='.',
command=lambda: press('.'), height=1, width=7)
Decimal.grid(row=6, column=2)
gui.mainloop()
【问题讨论】:
-
要退出,请尝试
gui.destroy。除此之外,我不明白你问题的另一部分。你所有的Entry都有相同的textvariable,这是故意的吗?
标签: python tkinter button widget numpad