0

In my Tkinter project, there is one functionality which goes like this:

  1. User clicks on a button
  2. A new settings window opens
  3. The settings window has it's own widgets and related functions

I used the Toplevel call from Tkinter to create a new window. The problem that occurs is that even though I am able to see the widgets on the new window, they aren't working. So far I have a button with an image on it. The image does not appear (I tested for the path and it is correct), and also nothing happens when I click it. I have tested this window as a seperate Tkinter project so I know it works. It does not work when called from a different file.

This is the main code:

from tkinter import*
from datetime import datetime
from tkinter import ttk
import os

import settingsController

root = Tk()
root.title("Main")
root.geometry("480x320") 
root.resizable(0, 0) 
root.configure(background="white")
root.overrideredirect(0)

settings_logo = PhotoImage(file="settings_icon.gif")
settings_button = Button(root, image=settings_logo,command=lambda: settingsController.begin(root))
settings_button.place(x= 277, y= 220)

settings_label = Label(root, fg = "black", font = "Times 10 bold", text="Settings", background= "white")
settings_label.place(x = 274, y = 275, width= 60, height= 25)

root.mainloop()

And this is the relevant part of the settingsController

from tkinter import *
root = None
def begin(rootWindow):

    global root

    root = Toplevel(rootWindow)

    root.attributes('-fullscreen', True)
    # root.geometry("400x400")

    # root.title("Settings")
    root.configure(bg="white")

    screen_width = rootWindow.winfo_screenwidth()
    screen_height = rootWindow.winfo_screenheight()

    img = PhotoImage(file="icon_50x50.png")

    btn = Button(root,image=img,bg="white",command=foo,height=50,width=50)
    btn.place(x=0,y=0)

    print("Screen width:", screen_width)
    print("Screen height:", screen_height)
    

How do I accomplish this? This is a big project and I've been specifically asked to create seperate files, so I cannot dump all of this in just 1 file.

RishiC
  • 768
  • 1
  • 10
  • 34
  • I think you have to add a ```root.mainloop()``` in the ```settingsController``` –  Aug 13 '21 at 05:07
  • Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – acw1668 Aug 13 '21 at 05:14
  • 1
    @Sujay: you should only call `mainloop` a single time for the life of the program. – Bryan Oakley Aug 13 '21 at 05:15

1 Answers1

0

The script is initializing ktinker again. You need to import the second script from the first script and then run a function in the second script with 'root' as a parameter.

Notes:

This is an example and you can add your code to it (I am using your script 2). This code considers that both of the scripts are in the same directory.

script1.py:

from tkinter import*
from datetime import datetime
from tkinter import ttk

root = Tk()

import script2
script2.begin(root, rootWindow)

script2.py:

from tkinter import *
def begin(root, rootWindow):
    root.attributes('-fullscreen', True)
    # root.geometry("400x400")

    # root.title("Settings")
    root.configure(bg="white")

    screen_width = rootWindow.winfo_screenwidth()
    screen_height = rootWindow.winfo_screenheight()

    img = PhotoImage(file="icon_50x50.png")

    btn = Button(root,image=img,bg="white",command=foo,height=50,width=50)
    btn.place(x=0,y=0)

    print("Screen width:", screen_width)
    print("Screen height:", screen_height)

So basically what I did here was pass the root that you initialized in the first script to the second script by calling a function in the second script from the first script.

acw1668
  • 40,144
  • 5
  • 22
  • 34
Rushil Gupta
  • 182
  • 4