Python Forum
[Tkinter] Tkinter GUI gets very slow and stuck
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter GUI gets very slow and stuck
#11
(Jun-16-2020, 01:06 AM)deanhystad Wrote: What does the rd_dig_in code look like? I cannot find the Modbus_PLC package.

I had the same problem with not finding any information on it
Reply
#12
Ok ill post the full code here.
The Modbus_PLC.py code :
from pyModbusTCP.client import ModbusClient

import numpy as np

c = ModbusClient(port = 502, auto_open=True)



#read descrete inputs:
def rd_dig_in(address, value):
    dig_in = c.read_discrete_inputs(address, value)
The Main GUI function:
import Modbus_PLC as md
from tkinter import *


class DigitalInputLamp():

    def __init__(self, label, img1, img2):
        # Are images same for all lamps?  Make a class variable?
        self.images = [img1, img2]
        self.label = label

        # Placing widget should probably happen outside the class
        # when the widget is created

        self.state = 0
        self.label.config(image=self.images[self.state])

    def show_state(self, state):

        if state != self.state:
            self.state = state
            self.label.config(image=self.images[self.state])

def update_lamps(period,lamps,canvas):
    digital_inputs = md.rd_dig_in(0, len(lamps))
    print(digital_inputs)
    for i, lamp in enumerate(lamps):
        lamp.show_state(digital_inputs[i])
    canvas.after(period,lamps)


def Main_Page():

    root1 = Tk()
    root1.title("GUI App")

    canvas44 = Canvas(root1, height=512, width=898)
    canvas44.pack()
    img_butt8 = PhotoImage(file='C:\\Users\\Omer\\Desktop\\Green_led1.png')
    img_butt9 = PhotoImage(file='C:\\Users\\Omer\\Desktop\\Red_led1.png')
    label1 = Label(canvas44)
    label2 = Label(canvas44)
    label3 = Label(canvas44)
    label4 = Label(canvas44)
    label5 = Label(canvas44)
    # label_list=[label1,label2,label3,label4,label5]
    # Make a bunch of lamps and start updating
    label1.place(x=0, y=10)
    label2.place(x=80, y=10)
    label3.place(x=160, y=10)
    label4.place(x=230, y=10)
    label5.place(x=280, y=10)
    lamps=[]
    lamps.append(DigitalInputLamp(label1, img_butt8, img_butt9))
    lamps.append(DigitalInputLamp(label2, img_butt8, img_butt9))
    lamps.append(DigitalInputLamp(label3, img_butt8, img_butt9))
    update_lamps(500, lamps, canvas44)
    root1.mainloop()



def IP_add_page():
    root4 = Tk()
    root4.title("IP Address")

    canvas5 = Canvas(root4, height=120, width=250, bg='#143660')
    canvas5.pack()
    ip_var = StringVar()


    def submit():
        x = str(entry_ip.get())
        ip_var.set("")
        md.c.host(x)




    def close_IP_window():
        root4.destroy()
        print("closing windows")

    entry_ip = Entry(canvas5, textvariable=ip_var,font='Arial 15 bold' )
    entry_ip.place(x=15, y=45)
    entry_ip.focus_force()
    button44 = Button(canvas5, font='Arial 10 bold', text='SUBMIT', command=lambda : [submit(), close_IP_window(),Main_Page()] , relief=GROOVE)
    button44.place(x=5, y=91)
    label_ip1=Label(canvas5, font='Arial 12 bold', text="Enter the PLC IP address: ", bg='#143660', fg='white')
    label_ip1.place(x=5, y=10)
    root4.resizable(False, False)



    root4.mainloop()

IP_add_page()
Once i run the code it supposed to open the first window that asks the user to the IP address , once the user enters the IP address and click on the 'SUBMIT' button another window pop up that shows the digital input indications (as Lamps) , The digital input registers are already set in the PLC.
Thanks.
Reply
#13
where did you get pyModbusTCP from, where is its documents page showing details about its methods?
Reply
#14
Here is the pyModbus documentation :
pyModbus documentation PDF link

im trying yo understand why digital_inputs = md.rd_dig_in(0, len(lamps)) returns None , it supposed to return an array of len(lamps) - 1 elements.
my old code works fine and it actually read the array (the only problem was , the gui is very slow)g
I wonder why in this current code it returns None instead of that array...
Reply
#15
Ok i think i solved the problem of that function return None.
i used md.c.read_discrete_inputs(0,len(lamps))instead of md.rd_dig_in and i received a list of digital input
def update_lamps(lamps,period,canvas):
    #reading the digital input registers
    digital_inputs = md.c.read_discrete_inputs(0, len(lamps))
    print(digital_inputs)
    for i, lamp in enumerate(lamps):
        # lamp is a class object element and it uses its class internal function show_state
        lamp.show_state(digital_inputs[i])
    canvas.after(period, lamps)
although i get the following error:

Output:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Omer\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:/Users/Omer/test.py", line 96, in <lambda> button44 = Button(canvas5, font='Arial 10 bold', text='SUBMIT', command=lambda : [submit(), close_IP_window(),Main_Page()] , relief=GROOVE) File "C:/Users/Omer/test.py", line 66, in Main_Page update_lamps(lamps, 500, canvas44) File "C:/Users/Omer/test.py", line 30, in update_lamps canvas.after(period, lamps) File "C:\Users\Omer\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 755, in after callit.__name__ = func.__name__ AttributeError: 'list' object has no attribute '__name__'
I think it finishes the first iteration but receive that error at the second iteration.
coz before the error shows up i get the list :
Output:
C:\Users\Omer\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/Omer/test.py closing windows [True, False, True, False, False, False, False, False, False, False]
Reply
#16
The "after" call is bad. It needs to provide a function: canvas.after(ms, func, *args). It should be "canvas.after(period, update_lamps, lamps, period, canvas)". This will make "update_lamps" call itself over and over.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Button widget gets stuck from using tkinter.messagebox? Nwb 2 3,897 Jun-20-2018, 02:21 PM
Last Post: Nwb

Forum Jump:

User Panel Messages

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