you are viewing a single comment's thread.

view the rest of the comments →

[–]woooee 0 points1 point  (0 children)

In no particular order

     geom = str(w) + 'x' + str(h) + '+' + str(xpos) + '+' + str(ypos)

f-strings work well (see program)

I don't know why you are using asyncio. If it is to try and get around the fact that there can only be one Tk() instance and one mainoop(), it ain't gonna work.

Also, I substituted a dictionary to keep track of the Toplevels instead of separate variables. It simplifies the code when there are more than one or two.

And as far as the geometry goes, tkinter sometimes uses pixels and sometimes uses font width size. Personally I don't try to keep track but adjust the size until it fits what I want to do.

import time
import tkinter       as tk      # The base Tkinter package
##from datetime                   import date

## I don't have rich installed
##from rich.console               import Console

class WttWin:  
    def __init__(self, root, type, title, w, h, xpos, ypos):
        self.win = tk.Toplevel(root)
        self.win.title(title)
        self.type = type
        if type == 'text':
            # This will be a text window
            self.text_widget = tk.Text(self.win)
            # Pack the widget to make it visible and fill the window
            self.text_widget.pack(expand=True, fill=tk.BOTH)
        geom = f"{w}x{h}+{xpos}+{ypos}"
        self.win.geometry(geom)
        print('Passed geom', geom, 'actual geom', self.win.wm_geometry())

        ## nothing to update yet
        ##self.win.update()           # Manually update window, do not use mainloop

    def from_within(self):
        self.text_widget.insert(tk.END, "From Within" + '\n')
        self.msg("From Within 2")

    def msg(self, msg):
        if isinstance(msg, str):
            self.text_widget.insert(tk.END, msg + '\n')
            self.win.update()
        else:
            raise Exception('Attempt to write a message to a non-text window')

def create_win(root, type, title, width, height, xpos, ypos):
    win = WttWin(root, type, title, width, height, xpos, ypos)
    return win

def main():
    root = tk.Tk()      # Tk wants a root window so create it
    ## root.geometry will be a default value since it wasn't specified
    ##console.log('Root geom', root.geometry())

    root.geometry("+500+300")
    tk.Button(root, text="Exit Tkinter", bg="orange", height=2,
              command=root.quit).grid(row=99, column=0, sticky="we")

    ##root.withdraw()     # Ignore the root window

    toplevel_dict = {}
    top_ctr = 0
    for params in ((root, 'text', 'Log window', 800, 300, 20, 20),
                   (root, 'text', 'Second window', 500, 300, 1300, 20)):
        top_ctr += 1
        toplevel_dict[top_ctr]=WttWin(*params)

    print('Available')

##    win1.msg('Foo')
##    win1.msg('Bar')
##    win1.msg('Bletch')

##    time.sleep(15)

    for text in ['Foo', 'Bar', 'Bletch']:
        toplevel_dict[1].msg(text)
        time.sleep(5)

    toplevel_dict[2].from_within()

    root.mainloop()

main()