Python Forum

Full Version: tkinter help please
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
hi guys!
why this code doesn't work as I want ?
I've tried several ways but it does the same,
it shows the scrolled bar widget at the end of the procedure

from tkinter import *
import tkinter as tk
from tkinter import scrolledtext
import os
import keyboard
import serial
import time

def on_closing():
	pass

def Ready():
	keyboard.wait('y'or 'Y')
	ser4.open()
	ser4.write(b'\x00')
	ser4.write(b'\x00')
	ser4.close()

	
def PS1_5():
	ser9.open()
	test_area.insert(tk.END,str("-- Going to Remote Mode. \n"))
	ser9.write("SESS01\r".encode())
	ser9.write("SESS01\r".encode())
	test_area.insert(tk.END,str("-- Setting up the voltage to +5.0 Vdc. \n"))
	ser9.write("VOLT00050\r".encode())
	test_area.insert(tk.END,str("-- Closing output switch. \n"))
	ser9.write("SOUT000\r".encode())
	ser9.write("SOUT000\r".encode())
	MEAS_1=ser9.write("GETD\r".encode())
	# 500 ms delay for instrument read
	time.sleep(.5)
	test_area.insert(tk.END,str("-- PS1 reporting: "))	
	test_area.insert(tk.END,(MEAS_1))
	test_area.insert(tk.END,str(" volts\n"))	
	test_area.insert(tk.END,str("-- Connecting DMM to PS1 entering the console.\n"))
	test_area.insert(tk.END,str("-- Everything ok? Please prees Y or N...\n"))
	ser9.close()
	ser4.open()
	ser4.write(b'\x03')
	ser4.write(b'\x03')
	ser4.close()
	test_area.yview(END)

def PS1_15():	
	ser9.open()
	test_area.insert(tk.END,str("-- Going to Remote Mode. \n"))
	ser9.write("SESS01\r".encode())
	ser9.write("SESS01\r".encode())
	test_area.insert(tk.END,str("-- Setting up the voltage to +15.0 Vdc. \n"))
	ser9.write("VOLT00150\r".encode())
	test_area.insert(tk.END,str("-- Closing output switch. \n"))
	ser9.write("SOUT000\r".encode())
	ser9.write("SOUT000\r".encode())
	MEAS_2=ser9.write("GETD\r".encode())
	# 500 ms delay for instrument read
	time.sleep(.5)
	test_area.insert(tk.END,str("-- PS1 reporting: "))	
	test_area.insert(tk.END,(MEAS_2))
	test_area.insert(tk.END,str(" volts\n"))	
	test_area.insert(tk.END,str("-- Connecting DMM to PS1 entering the console.\n"))
	test_area.insert(tk.END,str("-- Everything ok? Please prees Y or N...\n"))
	ser9.close()
	ser4.open()
	ser4.write(b'\x03')
	ser4.write(b'\x03')
	ser4.close()
	test_area.yview(END)

def PS1_25():	
	ser9.open()
	test_area.insert(tk.END,str("-- Going to Remote Mode. \n"))
	ser9.write("SESS01\r".encode())
	ser9.write("SESS01\r".encode())
	test_area.insert(tk.END,str("-- Setting up the voltage to +28.0 Vdc. \n"))
	ser9.write("VOLT00280\r".encode())
	test_area.insert(tk.END,str("-- Closing output switch. \n"))
	ser9.write("SOUT000\r".encode())
	ser9.write("SOUT000\r".encode())
	MEAS_3=ser9.write("GETD\r".encode())
	# 500 ms delay for instrument read
	time.sleep(.5)
	test_area.insert(tk.END,str("-- PS1 reporting: "))	
	test_area.insert(tk.END,(MEAS_3))
	test_area.insert(tk.END,str(" volts\n"))	
	test_area.insert(tk.END,str("-- Connecting DMM to PS1 entering the console.\n"))
	test_area.insert(tk.END,str("-- Everything ok? Please prees Y or N...\n"))
	ser9.close()
	ser4.open()
	ser4.write(b'\x03')
	ser4.write(b'\x03')
	ser4.close()
	test_area.yview(END)
	
def PS1_OUT():	
	ser9.open()
	test_area.insert(tk.END,str("-- Going to Remote Mode. \n"))
	ser9.write("SESS01\r".encode())
	ser9.write("SESS01\r".encode())
	test_area.insert(tk.END,str("-- Setting up the voltage to +28.0 Vdc. \n"))
	ser9.write("VOLT00100\r".encode())
	test_area.insert(tk.END,str("-- Closing output switch. \n"))
	ser9.write("SOUT000\r".encode())
	ser9.write("SOUT000\r".encode())
	MEAS_3=ser9.write("GETD\r".encode())
	# 500 ms delay for instrument read
	time.sleep(.5)
	test_area.insert(tk.END,str("-- PS1 reporting: "))	
	test_area.insert(tk.END,(MEAS_3))
	test_area.insert(tk.END,str(" volts\n"))	
	test_area.insert(tk.END,str("-- Connecting DMM to ADAPTER.\n"))
	test_area.insert(tk.END,str("-- Everything ok? Please prees Y or N...\n"))
	ser9.close()
	ser4.open()
	ser4.write(b'\x0B')
	ser4.write(b'\x0B')
	ser4.close()
	test_area.yview(END)
		
def update_blue():
    mylabel.configure(fg = "blue", text = "This is some blue text")
    mylabel.pack(pady = 5)
    
def update_red():
    mylabel.configure(fg = "red", text = "This is some red text")
    mylabel.pack(pady = 5)

def leaving():
	ser9.open()
	ser9.write("VOLT00000\r".encode())
	ser9.write("SOUT000\r".encode())
	ser9.write("SOUT001\r".encode())
	ser9.close()
	#ser10.open()
	#ser10.write("VOLT00000\r".encode())
	#ser10.write("SOUT000\r".encode())
	#ser10.write("SOUT001\r".encode())
	#ser10.close()
	ser4.open()
	ser4.write(b'\x00')
	ser4.close()
	root.destroy()
	#Instruments.devices()
		 
root = Tk()
root.geometry('1000x950+450+70')
root.title("AC/DC Power Supplies Verification Procedure")
root.resizable(False, False)
root.attributes('-topmost', 1)	#always on top
root.protocol("WM_DELETE_WINDOW", on_closing)	
root.focus_set()

#Initializing and opening serial ports
ser4=serial.Serial(port='COM4', baudrate=9600, timeout=1)
ser9=serial.Serial(port='COM9', baudrate=9600, timeout=1)
ser9.close()
ser4.close()


# Title Label
tk.Label(root, 
         text = "ScrolledText Widget Example", 
         font = ("Arial", 15), 
         background = 'lightgray', 
         foreground = "white").grid(column = 0,
                                    row = 0)
			
test_area = scrolledtext.ScrolledText(root, 
                                      wrap = tk.WORD, 
                                      width = 100, 
                                      height = 20, 
                                      font = ("Arial",
                                              12))

test_area.grid(column = 0, pady = 10, padx = 10)
	
Button(root, text='Close',font = ('Arial', 12, "bold"), command = leaving).grid(pady = 10, padx = 10)	
	
test_area.insert(tk.END,("======================================================================\n"))
test_area.insert(tk.END,("P.N: BK Precision 1697        \n"))
test_area.insert(tk.END,("AC/DC Power Supplies Verification Procedure                           \n"))
test_area.insert(tk.END,("Verification Procedure Ver 1.0\n"))
test_area.insert(tk.END,("======================================================================\n"))
test_area.insert(tk.END,("DC Power Supply No.1                                                  \n"))
test_area.insert(tk.END,("COM9 will be open to get access to PS.1 and set voltages... \n"))
test_area.insert(tk.END,("COM4 will be used to connect DMM to read voltages.\n"))

mylabel = Label(root, text = "This is some black text")

#test_area.after(0, PS1_5)
#test_area.after(2000, Ready)
#test_area.after(4000, PS1_15)
#test_area.after(6000, Ready)
#test_area.after(8000, PS1_25)
#test_area.after(10000, Ready)
#test_area.after(12000, PS1_OUT)
#test_area.after(14000, Ready)

#PS1_5
ser9.open()
test_area.insert(tk.END,str("-- Going to Remote Mode. \n"))
ser9.write("SESS01\r".encode())
ser9.write("SESS01\r".encode())
test_area.insert(tk.END,str("-- Setting up the voltage to +5.0 Vdc. \n"))
ser9.write("VOLT00050\r".encode())
test_area.insert(tk.END,str("-- Closing output switch. \n"))
ser9.write("SOUT000\r".encode())
ser9.write("SOUT000\r".encode())
MEAS_1=ser9.write("GETD\r".encode())
# 500 ms delay for instrument read
time.sleep(.5)
test_area.insert(tk.END,str("-- PS1 reporting: "))	
test_area.insert(tk.END,(MEAS_1))
test_area.insert(tk.END,str(" volts\n"))	
test_area.insert(tk.END,str("-- Connecting DMM to PS1 entering the console.\n"))
test_area.insert(tk.END,str("-- Everything ok? Please prees Y or N...\n"))
ser9.close()
ser4.open()
ser4.write(b'\x03')
ser4.write(b'\x03')
ser4.close()
test_area.yview(END)
#ready
keyboard.wait('y'or 'Y')
ser4.open()
ser4.write(b'\x00')
ser4.write(b'\x00')
ser4.close()

ser9.open()
test_area.insert(tk.END,str("-- Going to Remote Mode. \n"))
ser9.write("SESS01\r".encode())
ser9.write("SESS01\r".encode())
test_area.insert(tk.END,str("-- Setting up the voltage to +10.0 Vdc external. \n"))
ser9.write("VOLT00100\r".encode())
test_area.insert(tk.END,str("-- Closing output switch. \n"))
ser9.write("SOUT000\r".encode())
ser9.write("SOUT000\r".encode())
MEAS_3=ser9.write("GETD\r".encode())
# 500 ms delay for instrument read
time.sleep(.5)
test_area.insert(tk.END,str("-- PS1 reporting: "))	
test_area.insert(tk.END,(MEAS_3))
test_area.insert(tk.END,str(" volts\n"))	
test_area.insert(tk.END,str("-- Connecting DMM to ADAPTER.\n"))
test_area.insert(tk.END,str("-- Everything ok? Please prees Y or N...\n"))
ser9.close()
ser4.open()
ser4.write(b'\x0B')
ser4.write(b'\x0B')
ser4.close()
test_area.yview(END)
#ready
keyboard.wait('y'or 'Y')
ser4.open()
ser4.write(b'\x00')
ser4.write(b'\x00')
ser4.close()

root.mainloop()
See response in the other place you posted this code. The short answer is that GUI code is not written the same way as a script. You need to create your window and start the event loop (call mainloop()). Nothing appears to happen before you call mainloop() because mainloop() is responsible for updating windows.
(Mar-24-2023, 05:35 PM)deanhystad Wrote: [ -> ]See response in the other place you posted this code. The short answer is that GUI code is not written the same way as a script. You need to create your window and start the event loop (call mainloop()). Nothing appears to happen before you call mainloop() because mainloop() is responsible for updating windows.

done
I moved the windows opening just to the beginning and the codes does the same thing
What do you mean by "moved the windows opening". You do not open windows. root.mainloop() opens windows. There will be no indication of anything happening in a tkinter application before root.mainloop is called. Once root.mainloop() is called, creating a new window will result in it appearing almost immediately, because mainloop() will see there is a new window, and it will draw the window to the screen.

Of course there is the small problem that mainloop() is blocking. It does not return untill all windows are closed. I suppose you could do something like this:
import tkinter

# Make your first window
root = tkinter.Tk()
tkinter.Label(root, text="1").pack()
root.mainloop()  # Will wait here until you close the root window

# make your second window
root = tkinter.Tk()
tkinter.Label(root, text="2").pack()
root.mainloop()
I don't know why anyone would do this instead of writing individual programs that could be run in any order, but this does follow the tkinter pattern of making the window, calling mainloop(), wait for windows to close. Continue with remainder of program (usually there is nothing after mainloop()),
(Mar-30-2023, 02:50 AM)deanhystad Wrote: [ -> ]What do you mean by "moved the windows opening". You do not open windows. root.mainloop() opens windows. There will be no indication of anything happening in a tkinter application before root.mainloop is called. Once root.mainloop() is called, creating a new window will result in it appearing almost immediately, because mainloop() will see there is a new window, and it will draw the window to the screen.

Of course there is the small problem that mainloop() is blocking. It does not return untill all windows are closed. I suppose you could do something like this:
import tkinter

# Make your first window
root = tkinter.Tk()
tkinter.Label(root, text="1").pack()
root.mainloop()  # Will wait here until you close the root window

# make your second window
root = tkinter.Tk()
tkinter.Label(root, text="2").pack()
root.mainloop()
I don't know why anyone would do this instead of writing individual programs that could be run in any order, but this does follow the tkinter pattern of making the window, calling mainloop(), wait for windows to close. Continue with remainder of program (usually there is nothing after mainloop()),

not exactly
back to trying....
what I want is to open a window with scrolled text widget
and I want first of all to be able to see that window, then...
after every time I press the Y key in my keyboard I want to see the result in the scrolled down text.
the code functions ok but the window is shown at the end.
I told you why the code in your OP works the way it does (window not showing up until the end). I provided an example that showed how you could run "scripts" from a tkinter GUI. I informed you that you cannot use the keyboard module with tkinter, so you are not going to have a program that does this:
Quote:after every time I press the Y key in my keyboard I want to see the result in the scrolled down text.
What are you looking to achieve here? I have not seen any code since your first post.
(Apr-03-2023, 07:51 PM)deanhystad Wrote: [ -> ]I told you why the code in your OP works the way it does (window not showing up until the end). I provided an example that showed how you could run "scripts" from a tkinter GUI. I informed you that you cannot use the keyboard module with tkinter, so you are not going to have a program that does this:
Quote:after every time I press the Y key in my keyboard I want to see the result in the scrolled down text.
What are you looking to achieve here? I have not seen any code since your first post.

ok I think I have the idea, do not use the keyboard module with tkinter looks to be my problem,
I reiterate, I don't need two windows, I only need one window with the scrolled text widget running down every time I answer the question
do you suggest any other module that works for my code?
Did you read this post? Did you try the code?

https://python-forum.io/thread-29592-pos...#pid168021

It simulates have a serial connection, so it obviously won't talk to whatever hardware is on the other end of the wire, but it is organized in a way that allows running a "script-like" thing from a GUI.

You haven't posted any code since March 23. It is difficult to help when I have no idea what you are doing.
(Apr-04-2023, 05:56 PM)deanhystad Wrote: [ -> ]Did you read this post? Did you try the code?

https://python-forum.io/thread-29592-pos...#pid168021

It simulates have a serial connection, so it obviously won't talk to whatever hardware is on the other end of the wire, but it is organized in a way that allows running a "script-like" thing from a GUI.

You haven't posted any code since March 23. It is difficult to help when I have no idea what you are doing.


Very interesting
learning a lot
remember I'm not a professional programmer.
made a lot of progress,
never saw this runner method before,
how can I keep at the end of the scrolledtext ?
yview method doesn't work
ok
just learning
Pages: 1 2