Karl
Karl

Reputation: 5733

Invoking "package require Tk" in Tcl without writing it in every .tcl files

I am learning Tcl/Tk, and I have been following a tutorial to a point that I am moving to Tk. All the files in the tutorial use Tk commands (such as "button") right away, without having "package require Tk" written in the .tcl files. I am executing this from the console that accepts user input with "%" in the front.

If the tutorials don't have that "package require Tk", am I missing something? How can we have Tk without having to write "package require Tk" in all the files, or am I doing it wrong?

Upvotes: 2

Views: 885

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137577

If you've associated the file extension .tcl with the wish executable (modulo possible extensions and version info, of course) then you'll effectively have a package require Tk done for you before your script starts running. This is not recommended as being good practice in modern code — make your dependencies explicit, please — but it works just fine and many people do it.

It's not recommended that you force the matter by editing Tcl's startup scripts. After all, there's applications out there that use Tcl without Tk.

Upvotes: 5

Related Questions