Python Forum
need pulse counter in GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need pulse counter in GUI
#1
Hello everyone,
I am new on Tkinter, I have python program which is I want to convert to GUI, Its simple pulse counter using Raspberry pi 3 and Nodemcu. Nodemcu is a client and raspberry pi 3 is the server. I want GUI following image type.
[Image: 9ktl6a.jpg]

Here is my python code
import paho.mqtt.client as mqtt
import time
y=0


def on_connect(client, userdata, flags, rc):
	
	print("Connected with result code "+str(rc))
	
	client.subscribe("/esp/pot") 

def on_message(client, userdata, msg):
	
	tag=msg.payload
	global count,x,y
	if(msg.payload=="N1" or msg.payload=="N2"  or msg.payload=="N3"  or msg.payload=="N4"  or msg.payload=="N5"  or msg.payload=="N6"  or msg.payload=="N7"  or msg.payload=="N8"  or msg.payload=="N9"  or msg.payload=="N10"  or msg.payload=="N11"  or msg.payload=="N12"  or msg.payload=="N13"  or msg.payload=="N14"  or msg.payload=="N15"  or msg.payload=="N16"  or msg.payload=="N17"  or msg.payload=="N18"  or msg.payload=="N19"  or msg.payload=="N20"  or msg.payload=="N21"  or msg.payload=="N22"  or msg.payload=="N23"  or msg.payload=="N24"  or msg.payload=="N25"  or msg.payload=="N26"  or msg.payload=="N27"  or msg.payload=="N28"  or msg.payload=="N29"  or msg.payload=="N30" ):
		if msg.payload== "N1":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N1", (y));
			print ("N1")
			print (y)
			client.disconnect();
			y=0

			
		if msg.payload=="N2":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N2", (y));
			print ("N2")
			print (y)
			client.disconnect();
			y=0

	
		if msg.payload== "N3":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N3", (y));
			print (y)
			client.disconnect();
			y=0
			
		if msg.payload== "N4":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N4", (y));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N5":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N5", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N6":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N6", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N7":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N7", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N8":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N8", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N9":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N9", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N10":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N10", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N11":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N11", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N12":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N12", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N13":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N13", (y/10));
			print (y/10)
			client.disconnect();
			y=0
		
		if msg.payload== "N14":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N14", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N15":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N15", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N16":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N16", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N17":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N17", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N18":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N18", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N19":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N19", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N20":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N20", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N21":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N21", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N22":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N22", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N23":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N23", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N24":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N24", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N25":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N25", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N26":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N26", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N27":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N27", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N28":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N28", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N29":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N29", (y/10));
			print (y/10)
			client.disconnect();
			y=0
			
		if msg.payload== "N30":
			client = mqtt.Client()
			client.connect("localhost",1883,60)
			client.publish("/feeds/N30", (y/10));
			print (y/10)
			client.disconnect();
			y=0

	else:
		count=int(msg.payload)
		x=count
		y=(x+y)
		print y
		


		

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60) 
client.loop_forever()
in that code "N1 to N30" is board number,
Please help me how to write code for GUI
Reply
#2
At first you have to make up your mind on with which module you want to programm your GUI: TKinter, QT5, PySide2, etc..
Then start with a tutorial on that module, here are some useful links:
http://zetcode.com/gui/pyqt5/ (QT5 tutorial)
https://pythonspot.com/pyqt5/ (QT5 tutorial)
https://wiki.qt.io/PySide_Tutorials (PySide tutorial)
https://www.python-course.eu/python_tkinter.php (Tkinter tutorial)
If you want to have an GUI builder at hand, check out this link (where it says "GUI Design Tools and IDEs"):
https://wiki.python.org/moin/GuiProgramming
Some specs are very important to know, like: which python version do you use? what shall be done with the data? which buttons do you need? do you need it to be functional or pretty, or both? How do you prefer the data to be presented? Do you want you data printed in an Label, Image, LCDNUmber (Qt), etc.?
As an adviced I would say: try to build a GUI on your own, you will see it is quite easy. If you need help ask :) Start by drawing the general layout an a piece of paper an then build everything :)
I hope the links help you, cheers :)
Reply
#3
I want to use TKinter and my python version is 3, No need any button on GUI just need runtime changing on GUI because when pules come it should count and show in GUI screen.
In GUI need only 3 labels
1. Total pulse count
2. Total Pulse sent
3. Board Number

That's it,
Reply
#4
Then click or write a small GUI with the given tutorials. I would suggest to have something checking for new data asynchroniously and update the labels via the .setText() function of the Label. When you have some GUI code it is easier to help you :)
Reply
#5
Thanks, "ThiefOfTime" Now I am using TKinter for GUI and successfully make small GUI see below image,
[Image: 2ewk2ti.jpg]
Now in that GUI showing proper pulse value in "Total Pulse Count" label but when the server sends all pulse to any board (Board name write in code N1 to N30) than other 2 label box "Total Pulse sent" and "No." not show.
please, anyone, help me find the solution for that
here is my code
from tkinter import *
import paho.mqtt.client as mqtt
import time
y = 0

def on_connect(client, userdata, flags, rc):
    global E1
    global E2
    global E3
    print ("Connected with result code" + str(rc))
    E2.set("Conencted with result code" + str(rc))
    client.subscribe("/esp/pot")

def on_message(client, userdata, msg):
    tag = msg.payload
    global count
    global x
    global y
    global E1
    global E2
    global E3
    if (msg.payload == "N1") or (msg.payload == "N2") or (msg.payload == "N3") or (msg.payload == "N4") or (msg.payload == "N5") or (msg.payload == "N6") or (msg.payload == "N7") or (msg.payload == "N8") or (msg.payload == "N9") or (msg.payload == "N10") or (msg.payload == "N11") or (msg.payload == "N12") or (msg.payload == "N13") or (msg.payload == "N14") or (msg.payload == "N14") or (msg.payload == "N15") or (msg.payload == "N16") or (msg.payload == "N17") or (msg.payload == "N18") or (msg.payload == "N19") or (msg.payload == "N20") or (msg.payload == "N21") or (msg.payload == "N22") or (msg.payload == "N23") or (msg.payload == "N24") or (msg.payload == "N25") or (msg.payload == "N26") or (msg.payload == "N27") or (msg.payload == "N28") or (msg.payload == "N29") or (msg.payload == "N30"):
        if msg.payload == "N1":
            client = mqtt.Client()
            client.connect("localhost",1883,60)
            client.publish("/feeds/N1", (y));
            print (y)
            E3.set(y)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N2":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N2", (y));
            print (y)
            E3.set(y)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N3":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N3", (y));
            print (y)
            E3.set(y)
            Window.update_idletasks()
            client.disconnect();
            
        if msg.payload == "N4":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N4", (y));
            print (y/10)
            z = str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N5":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N5", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();
            
        if msg.payload == "N6":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N6", (y/10));
            print (y/10)
            z = str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();
            
        if msg.payload == "N7":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N7", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N8":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N8", (y/10));
            print (y/10)
            z = str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N9":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N9", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N10":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N10", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N11":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N11", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N12":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N12", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N13":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N13", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N14":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N14", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N15":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N15", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N16":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N16", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N17":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N17", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N18":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N18", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N19":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N19", (y/10));
            print (y/10)
            z =str(y/10)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N20":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N20", (y/10));
            print (y/10)
            z =str(y/10)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N21":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N21", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N22":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N22", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N23":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N23", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N24":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N24", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();
            
        if msg.payload == "N25":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N25", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N26":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N26", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N27":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N27", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N28":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N28", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();
            
        if msg.payload == "N29":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N29", (y/10));
            print (y/10)
            z =str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();

        if msg.payload == "N30":
            client = mqtt.Client()
            client.connect("localhost",1833,60)
            client.publish("/feeds/N30", (y/10));
            print (y/10)
            z = str(y/10)
            E1.set(z)
            Window.update_idletasks()
            client.disconnect();
            y = 0
    else:
        count = int(msg.payload)
        x = count
        y = (x+y)
        print (y)
        E1.set(y)
        Window.update_idletasks()

Window = Tk()
E1 = StringVar()
E2 = StringVar()
E3 = StringVar()

E3.set("NONE")
E2.set("NONE")
E1.set("NONE")


Total_PC_Label = Label(Window, text = "Total Pulse Count").grid(row=1, column=1)
Total_PC_Entry = Entry(Window, textvariable = E1).grid(row=2, column=1, ipady = 10, ipadx = 10, pady = 10, padx = 10)

Total_PS_Label = Label(Window, text = "Total Pulse Send").grid(row=3, column=1)
Total_PS_Entry = Entry(Window, textvariable = E2).grid(row=4, column=1, ipady = 10, ipadx = 10, pady = 10, padx = 10)

No_Label = Label(Window, text = "No.").grid(row=3, column =2)
No_Entry = Entry(Window, textvariable = E3).grid(row=4, column=2, ipady = 10, ipadx = 10, pady = 10, padx = 10)

client = mqtt.Client()
client.connect("localhost",1883,60)
client.on_connect = on_connect
client.on_message = on_message
Window.update_idletasks()
Window.update()
client.loop_forever()
Reply
#6
The "No." label is only changed, when the payload is N1, N2 or N3. In (nearly) any other case only the first label is changed. (you have two cases where nothing is changed, N19 and N20) It depends on what values are send from which Nxx parts. So the payload currently never reaches N1, N2 or N3
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020