简体   繁体   中英

Disconnect Multiple input at once in two entry widgets (tkinter)

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)

'''

As you have asked multiple questions. I will try to give answers question wise.

  1. 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? [Piyush] Same textvariable(equation) has been used for both entry widgets, as both entry widgets are seperate so different textvariable should be used for both entry widgets. I have edited the piece of code for both entry widgets which is mentioned below -

     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)
  2. I assume the problem is in the part of the press function and expression..? [Piyush] Above discussed entry widget issue is not occurring because of press function and expression defined within it. Root-cause of this issue is mentioned in above point.

  3. And also my Exit button wont close my window...? [Piyush] I checked the shared code by running it at my end and exit button is working as expected, it is closing the window. It is not working at your end, it might be because of the python environment(python version etc.). There is another way through which you can close the window, instead of using GUI.quit you can use GUI.destroy and see if it helps.

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)

'''

Here is the revise code which you can try -

def press(num):  
    global expression 
    expression = expression + str(num) 
    equation1.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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM