tkinter - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: tkinter (/thread-29592.html) Pages:
1
2
|
RE: tkinter - juliolop - Sep-16-2020 (Sep-15-2020, 03:38 PM)Knight18 Wrote: The most obvious mistake is that you're creating separate Tkinter sessions with Tk(). There's probably other things wrong with your code as well, but if you want to be creating several windows, you should use the top level widget. ok, thanks I got it they work great just missing one thing, what if in a toplevel window nonrelated to root, I have a button to open another similar toplevel window and at the same time I need to close the previous toplevel window if I choose to open the second window in command = open_toplevel2 I left the previous window open and I need to close it when the button is pressed, so basically I need to commands in one thx for your help (Sep-16-2020, 07:26 PM)juliolop Wrote:(Sep-15-2020, 03:38 PM)Knight18 Wrote: The most obvious mistake is that you're creating separate Tkinter sessions with Tk(). There's probably other things wrong with your code as well, but if you want to be creating several windows, you should use the top level widget. (Sep-16-2020, 06:53 PM)deanhystad Wrote: If you are just starting you should look for a tkinter tutorial that makes multiple windows. thanks but the tutorials I have only show opening new windows leaving the previous window open,like a cascade, and what I really need is to open a new window disappearing the previous one. thanks again RE: tkinter - deanhystad - Sep-17-2020 That is not a common user interface paradigm, which usually means it is a bad user interface paradigm. More common would be a single window that changes it's view. To do this you would create multiple frames and only show 1 at a time. The tkinter notebook widget could be used for this, but you can also "roll your own" interface if you don't like the notebook's tabbed interface. RE: tkinter - juliolop - Sep-23-2020 ok I used ttk notebook widget and I liked, certainly is really functional for what I wish to do, but after looking in several tutorials I can not find how to place a label widget or a simple text inside the particular tab window other than the tab "note" RE: tkinter - juliolop - Mar-22-2023 hi guys... what's wrong with this code? as you can see, I'm tested with different methods and do the same thing, it shows the windows at the end of the procedure, I want the window showing the messages one after another 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() RE: tkinter - deanhystad - Mar-24-2023 Use python tags when posting code. I don't understand why you are trying to write this as a GUI application. There is nothing going on that wouldn't work better as a console application. You are not following the correct pattern to use tkinter. In tkinter you make a root window and then run the root_window.mainloop() function. mainloop() is what makes the windows appear and buttons and entry objects work. And you won't be using the keyboard library either. The GUI should be getting the keystrokes. And you shouldn't have any code that blocks the GUI from running. Waiting for something in the test should not result in the GUI waiting. If you feel the need to write this as a tkinter application, I envision something like this: import time import threading import tkinter as tk from tkinter import scrolledtext, simpledialog, messagebox from functools import partial # from serial import Serial class Serial: """Fake serial driver so I can run tests""" def __init__(self, *args, **kvargs): self.volts = "0" pass def open(self): pass def close(self): pass def write(self, bytes): if bytes.startswith(b"VOLT"): self.volts = bytes[4:-1] window.print(f"writing {bytes}") return self.volts def voltage_test(runner, voltage=None): """Run a voltage test""" if voltage is None: voltage = runner.request( lambda: simpledialog.askfloat(title="Test", prompt="Enter voltage") ) ser9 = Serial(port="COM9", baudrate=9600, timeout=1) runner.print("-- Going to Remote Mode.") ser9.write("SESS01\r".encode()) ser9.write("SESS01\r".encode()) runner.print(f"-- Setting up the voltage to {voltage}.") ser9.write( f"VOLT{int(voltage*100):05}r".encode() ) # Is this the only part that changes? runner.print("-- Closing output switch.") ser9.write("SOUT000\r".encode()) ser9.write("SOUT000\r".encode()) MEAS_1 = ser9.write("GETD\r".encode()) # How does this get a value? time.sleep(0.5) runner.print( f"-- Commanding {voltage} volts", f"-- PS1 reporting: {MEAS_1.decode()} volts", "-- Connecting DMM to PS1 entering the console.", ) ser9.close() runner.request( lambda: messagebox.showinfo(title="Test", message="Press Ok to continue") ) ser4 = Serial(port="COM4", baudrate=9600, timeout=1) ser4.write(b"\x03") ser4.write(b"\x03") ser4.close() def help(runner): """Display help text""" runner.print( "======================================================================", "P.N: BK Precision 1697", "AC/DC Power Supplies Verification Procedure", "Verification Procedure Ver 1.0", "======================================================================", "DC Power Supply No.1 \n", "COM9 will be open to get access to PS.1 and set voltages...", "COM4 will be used to connect DMM to read voltages.", ) def countdown(runner, count, period=1): """Demonstrate that text is displayed while test runs""" for i in range(count, 0, -1): runner.print(str(i)) time.sleep(period) class MainWindow(tk.Tk): """A test runner""" columns = 4 def __init__(self): super().__init__() self.title("AC/DC Power Supplies Verification Procedure") self.label = tk.Label(self, text="Test Results") self.label.pack(padx=5, pady=5) self.text = scrolledtext.ScrolledText(self, wrap=tk.WORD, width=80, height=20) self.text.pack(padx=5, pady=5) self.tests = {} self.test_buttons = [] self.test_button_frame = tk.Frame(self) for column in range(self.columns): self.test_button_frame.grid_columnconfigure(column, weight=1, uniform="key") self.test_button_frame.pack(padx=5, pady=5, expand=True, fill=tk.X) self.request_action = None self.request_reply = None def add_test(self, title, func, *args): """Add a test to the menu""" self.tests[title] = partial(func, self, *args) self.test_buttons.append( tk.Button( self.test_button_frame, text=title, command=lambda: self.run_test(title) ) ) for index, button in enumerate(self.test_buttons): button.grid( row=index // self.columns, column=index % self.columns, sticky="news" ) def run_test(self, test_name): """Run selected test""" self.text.delete(1.0, tk.END) for button in self.test_buttons: button["state"] = tk.DISABLED self.thread = threading.Thread(target=self.tests[test_name]) self.thread.start() self.monitor_test() def monitor_test(self): """Monitor test execution. Executes any requests from the test. Enables test buttons when test is complete. """ if self.thread and self.thread.is_alive(): # Test is still running if self.request_action is not None: # execute the request self.request_reply = self.request_action() self.request_action = None self.after(100, self.monitor_test) else: # Test is complete self.thread = None for button in self.test_buttons: button["state"] = tk.NORMAL def print(self, *lines): """Print lines in scrolled text area.""" for line in lines: self.text.insert(tk.END, line + "\n") self.update() def request(self, action): """Use to open dialogs in the GUI. The test thread cannot do GUI things like open a dialog. action is a function closure that gets executed in the GUI thread. """ self.request_action = action while self.request_action is not None: time.sleep(0.1) return self.request_reply window = MainWindow() help(window) window.add_test("Help", help) window.add_test("Countdown", countdown, 5) window.add_test("Voltage Test", voltage_test) window.add_test("Voltage Test: 1.0V", voltage_test, 1) window.add_test("Voltage Test: 1.5V", voltage_test, 1.5) window.add_test("Voltage Test: 2.0V", voltage_test, 2) window.add_test("Voltage Test: 5.0V", voltage_test, 5) window.mainloop()The window is kind of a generic test runner, and you write tests as functions. The test functions are added to the runner using the "add_test()" method. This makes a button in the runner GUI. Pressing the button runs the test. The runner runs the test in a separate thread so you can do things like block waiting to read a serial port without it affecting how the GUI runs. The runner has a request function that lets the test pop up tkinter dialogs. This cannot be done in the test thread, so the request function passes the request on to the GUI thread which executes the request and returns the response. RE: tkinter - juliolop - Apr-05-2023 (Mar-24-2023, 03:25 AM)deanhystad Wrote: Use python tags when posting code. Quote:No I didn't see it until now, |