0

I am trying to create a python script that will trigger a tkinter window every time a certain event happens. The python script will have a while true loop and during the loop the tkinter event may or may not happen (if-else block). Right now the actual loop part isn't done, so I am currently testing the tkinter part but I can't seem to open more than tkinter window.

Below is the test script I am using.

from tkinter import *
from sys import exit
import os


onetwo = "C:/Users/I/Downloads/Transfer_Out_1016_Outlook.txt"

def popupError(s):
    popupRoot = Tk()
    ##popupRoot.after(20000, exit)
    popupButton = Button(popupRoot, text = s, font = ("Verdana", 12), bg = "yellow", command = lambda: os.system(onetwo))
    popupButton.pack()
    popupRoot.geometry('400x50+700+500')
    popupRoot.mainloop()


popupError("HelloWORLD")


def popupTwo(s):
    popupRoot = Tk()
    ##popupRoot.after(20000, exit)
    popupButton = Button(popupRoot, text = s, font = ("Verdana", 12), bg = "yellow", command = lambda: os.system(onetwo))
    popupButton.pack()
    popupRoot.geometry('400x50+700+500')
    popupRoot.mainloop()


popupTwo("HEWWWWWEWEWKOO")

I apologize for the lack of an actual piece of code but this is the best I can do right now given the dev status of the other parts of the overall python script.

Note that the tkinter window may be triggered more than once in a single loop session.

If any other details are needed, I'll try my best to add more in.

martineau
  • 119,623
  • 25
  • 170
  • 301
JLee
  • 1
  • 1
  • 4
    The best way to create additional windows is by creating [`Toplevel`](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/toplevel.html) instances. Creating multiple `Tk` instances is *discouraged*: See [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) – martineau Oct 18 '21 at 12:23
  • Also: when running Tkinter applications, you either should run **one** `root.mainloop` and write your code in events or once in a while call `root.update_idletasks()` in your busy loop. – tzot Oct 18 '21 at 12:51
  • `popupTwo()` will be executed after the first window is closed because of the `mainloop()` inside `popupError()`. – acw1668 Oct 18 '21 at 12:58

1 Answers1

0

Here’s what you can do:

from tkinter import *

def popup(winName):
    newWin = Toplevel()
    btn2 = Button(newWin, text=winName)
    btn2.pack()

root = Tk()

btn = Button(root, text=“Popup”, command=lambda: popup(“text”))
btn.pack()

root.mainloop()
InfoDaneMent
  • 326
  • 3
  • 18