Python Forum
Dual Tkinter windows and shells
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dual Tkinter windows and shells
#1
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()
Reply
#2
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).
Reply
#3
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
Reply
#4
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.
Reply
#5
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
Reply
#6
I am still missing the purpose. Why wouldn't such logic be written into the application?
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter two windows instead of one jacksfrustration 7 777 Feb-08-2024, 06:18 PM
Last Post: deanhystad
  pass a variable between tkinter and toplevel windows janeik 10 2,139 Jan-24-2024, 06:44 AM
Last Post: Liliana
  Tkinter multiple windows in the same window tomro91 1 788 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  [Tkinter] DUAL WINDOWS... ATARI_LIVE 5 2,963 Sep-27-2020, 11:35 AM
Last Post: ATARI_LIVE
  Tkinter scaling windows conten to or with its size not working Detzi 5 4,358 Jan-12-2020, 12:42 PM
Last Post: Detzi
  How to close one of the windows in Tkinter scratchmyhead 3 4,759 Dec-21-2019, 06:48 PM
Last Post: pashaliski
  Using tkinter on Windows to launch python 3 scripts Mocap 1 2,686 Jul-17-2019, 05:16 AM
Last Post: Yoriz
  [Tkinter] Using tkinter and mutiprocess ok on windows, locks up on ubuntu? ice 3 2,617 May-29-2019, 08:44 AM
Last Post: ice
  Int Variables in different Tkinter windows only returning 0 harry76 3 4,082 May-26-2019, 10:24 AM
Last Post: Yoriz
  [Tkinter] Ignore windows scaling in tkinter Gangwick 2 4,419 Jul-23-2018, 02:41 PM
Last Post: Gangwick

Forum Jump:

User Panel Messages

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