Dual Tkinter windows and shells - 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: Dual Tkinter windows and shells (/thread-29400.html) |
Dual Tkinter windows and shells - Astrikor - Aug-31-2020 I have two files with similar coding in each and I want to send data from one file to the other by calling it's function. I want the result to be a Tkinter window and a shell window generated from each file. By way of an example, I have created First.py and Second.py, with Second.py input to First.py. Running First.py starts a counter and after a few seconds the function in Seond.py is called. At this point I expected the Second.py Tkinter and shell windows to open. Unfortunately what happens is that Second.py overwrites both the First.py Tkinter and shell windows Can anyone please explain how to run both Tkinter and shell windows? The coding is as below (note that it is an example to illustrate the problem of running two independent but linked files. Note also that the files run perfectly if run separately with the Function in Second.py disabled). Many thanks Astrikor First.py: import Second from tkinter import * import time from datetime import datetime Count = 0 Posted = False #tkinter window setup: root = Tk() var = StringVar() var.set('') Label(root,textvariable = var, font=("ComicSans", 20)).pack() while True: Count=Count+1 LatestCount= Count ElapsedCount = (LatestCount)/5 if ElapsedCount == int(ElapsedCount) and Posted == True: Posted = False if Posted == False: Posted = True time.sleep(2) print("First ",Count, " ", LatestCount, " ", int(ElapsedCount)) p = "First ",datetime.now().strftime("%a %T") , " C:",str(Count), " EC:",str(ElapsedCount) separator = ' ' p= separator.join(p) var.set(p) root.update() if ElapsedCount == 5: Posted = True Second.OpenSecond(Posted,Count,ElapsedCount)Second.py: from tkinter import * import time from datetime import datetime #tkinter window setup: Posted = True Count = 0 ElapsedCount=0 root = Tk() var = StringVar() var.set('') Label(root,textvariable = var, font=("ComicSans", 20)).pack() p = "Second ",datetime.now().strftime("%a %T") , " C:",str(Count) separator = ' ' p= separator.join(p) var.set(p) root.update() #if Count >=0:# use this line and inhibit the next line to run Second.py independently def OpenSecond(Posted,Count,ElapsedCount): Posted = Posted FirstCount = Count ElapsedCount = ElapsedCount while True: Count=Count+1 LatestCount= Count ElapsedCount = (LatestCount)/10 if ElapsedCount == int(ElapsedCount) and Posted == True: Posted = False if Posted == False: Posted = True time.sleep(2) print("FirstCount:", FirstCount,"Second Count:",Count, " ", int(ElapsedCount)) p = "Second ",datetime.now().strftime("%a %T") , " C:",str(Count), " EC:",str(ElapsedCount) separator = ' ' p= separator.join(p) var.set(p) root.update() RE: Dual Tkinter windows and shells - deanhystad - Aug-31-2020 What are you trying to do? Are you trying to make an application with two windows? That's easy. Creating two windows is barely any more difficult than making one window. The problem you are having with the example is you call tkinter.Tk() twice. You cannot do that. Tk() creates the Tk application, and you can only have one of those. Calling a function in one module from another is pretty simple. If you want to write your two window application using two files (modules) that should be no problem. If the code for each window is very similar I think you should read about Python classes. You might see significant code reduction and end up not needing two files (modules). RE: Dual Tkinter windows and shells - Astrikor - Sep-01-2020 Thanks deanhystad, What I am trying to do is to run two completely separate versions of my code, but to be able to interchange data between them, and I was calling the function for that purpose. I'll have a look at classes. But any other suggestions appreciated! Astrikor RE: Dual Tkinter windows and shells - deanhystad - Sep-01-2020 I still don't understand what it is that you want to do. Run two slightly different versions of the same code at the same time does not mean anything. What do they do? How do they differ? Why do they need to communicate with each other? In some ways it sounds to me like you want two separate applications that talk to each other. You cannot do that with Tk. With Tk you can have an application with two similar windows, but it is still one application. To have two applications that talk to each other you need a communication medium like a shared file or some sort of serial connection. RE: Dual Tkinter windows and shells - Astrikor - Sep-01-2020 Consider it rather like a cycling peloton. (Both applications are accessing sales data from a URL). When one application runs out of steam (the turnover falls too far), the other takes over from the new lower baseline position by lowering sales margins, and the former continues in parallel until turnover recovers and the situation is then reversed in order to constantly track price and demand. So they need to communicate with each other. My example files were simply a hasty attempt to illustrate the concept of what I am trying to achieve. It could be done without the Tkinter window, but the advantage of that window (if it would work for both apps) is to continuously provide running displays of the current status of the two positions. Maybe a text file could be used to interchange the dataset, being constantly written and read by each app - as long as the read and write cycle could be arranged not to clash. Or maybe a subprocess module could do the job. But I still have to look at Python classes to see if there's anything relevant. Astrikor RE: Dual Tkinter windows and shells - deanhystad - Sep-01-2020 I am still missing the purpose. Why wouldn't such logic be written into the application? RE: Dual Tkinter windows and shells - Astrikor - Sep-03-2020 Problem solved by running the two files independently and inter communicating via text files. May not be the most elegant or fastest solution, but it works, giving me a Tkinter window for each file. Astrikor |