I need to program a GUI (touchscreen) with tkinter for Raspberry Pi. I have two entry widgets, which I need to enter seperately. Unfortunately both entries are written, when I enter a value on the numpad. How can change it, that I could only entry a value in the entry widget, which is selected? I assume the problem is in the part of the press function and expression..? And also my Exit button wont close my window...?
Thank you for your help!
This is my code:
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()
Here is my new code, which doesnt work:
'''
def press(num):
global expression
expression = expression + str(num)
equation.set(expression)
def clear():
global expression
expression = ""
equation1.set("")
equation2.set("")
#create GUI
# Driver code
if __name__ == "__main__":
# create a GUI window
gui = Tk()
gui.title("GUI")
equation1 = StringVar()
equation2 = StringVar()
equation1.set("")
equation2.set("")
#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=equation1, 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=equation2, width=4, bg ='#ffffff')
eingabe2.grid(row=1, column=1, padx=xdis, pady = ydis)
'''