0

I have 2 seperate files and trying to make a sort of 'module' with this one file. But it does not work.

The background color is still this weird gray color even if I set it to say, black. Here is the 'module' file:

import tkinter as tk
class TUICanv:
 global screenWidth, screenHeight
 def __init__(self,win,bg="#232627"):
 self.screenWidth = win.winfo_screenwidth()
 self.screenHeight = win.winfo_screenheight()
 self.canvas = tk.Canvas(win,width=self.screenWidth,height=self.screenHeight,background=bg)
 self.background = bg
 self.width = self.canvas.winfo_width()
 self.height = self.canvas.winfo_height()

And here is the other file:

import tkinter as tk
import terkinter as tr
import os
import re
win = tk.Tk()
allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"
win.title("Terminos")
canv = tr.TUICanv(win)
win.geometry(f'{canv.screenWidth}x{canv.screenHeight}')
canv.canvas.create_text(100,10,fill="darkblue",font="Times 20 italic bold",text="Click the bubbles that are multiples of two.")
win.mainloop()

The background does not change. It may be that the window in the other file is different, because I have only started tkinter 2 weeks ago.

asked Apr 24 at 15:11
3
  • 4
    You have not applied any layout manager (pack, grid or place) on the canvas, so it is hidden. Also the line global screenWidth, screenHeight is not necessary and better be removed. Another suggestion is to inherit TUICanv from tk.Canvas. Commented Apr 24 at 15:20
  • EDIT: Resolved: I added self.canvas.pack() Commented Apr 24 at 15:40
  • From design point of view, it is not recommended to call layout function inside child class because child does not know which layout manager is used in parent. So it is the responsibility of the parent to layout its children. Commented Apr 25 at 1:04

2 Answers 2

1

Your canvas is not visible. One way to make it visible is to pack it by by adding

self.canvas.pack(fill="both", expand=True)

to your terkinter.py __init__ method.

TheLizzard
7,8002 gold badges16 silver badges43 bronze badges
answered Apr 24 at 15:50
Sign up to request clarification or add additional context in comments.

1 Comment

It's not a good practice for the class to call pack on itself. That limits its usabiliity to only an application using pack. If the application switched to grid, this class would have to be modified.
0

As others have pointed out, the problem is that you aren't making the widget visible by calling pack, place, or grid, or adding to the screen in some other method.

The simple solution is for your app to call one of those methods. For example:

...
win.title("Terminos")
canv = tr.TUICanv(win)
canv.cavas.pack(side="top", fill="both", expand=True)
...

That's not the best solution though, since it depends on the calling app to know that it implements an inner canvas. A better solution would be for your class to inherit from tk.Canvas. In that way it behaves just like the canvas.

For example:

import tkinter as tk
class TUICanv(tk.Canvas):
 global screenWidth, screenHeight
 def __init__(self,win,bg="red"):
 self.screenWidth = win.winfo_screenwidth()
 self.screenHeight = win.winfo_screenheight()
 super().__init__(win,width=self.screenWidth, height=self.screenHeight,background=bg)
 self.width = self.winfo_width()
 self.height = self.winfo_height()
root = tk.Tk()
c = TUICanv(root)
c.pack(side="top", fill="both", expand=True)
root.mainloop()
answered Apr 25 at 14:51

1 Comment

Typo error canv.cavas should be canv.canvas

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.