Jun-16-2020, 05:55 AM
[Tkinter] Tkinter GUI gets very slow and stuck
[Tkinter] Tkinter GUI gets very slow and stuck
|
Jun-16-2020, 07:05 AM
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.
Jun-16-2020, 07:15 AM
where did you get pyModbusTCP from, where is its documents page showing details about its methods?
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...
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: 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 :
Jun-18-2020, 02:51 AM
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.
|
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
[Tkinter] Button widget gets stuck from using tkinter.messagebox? | Nwb | 2 | 4,825 |
Jun-20-2018, 02:21 PM Last Post: Nwb |
Users browsing this thread: 1 Guest(s)