Python and tkinter confusion - python

I am completely new to Python. The basics are pretty clear at the moment. I am trying to build a simple GUI for the first time and ran into tkinter. I copied a bit of code and ran it. It works.
from tkinter import *
from tkinter import ttk
root = TK()
root.title("test")
Then I wanted to alter and add code, created a new file and started typing:
from tkinter import *
from tkinter ttk
root = TK()
root.title("smartDisplay")
And guess what... It doesn't work. It says "name 'TK' is not defined". It is the same Mac the same IDE, the same folder of the two files. So what is going on here? What am I missing?
I am using python 3.8.2 on a Mac with 10.15.7

Python is case sensitive and needs root = Tk(). Also I didn't see a root.mainloop() at the end of your code. That is also needed to run the Tkinter window.

Related

tkinter in python not working and no web fix works

I am new to python and I am having a problem with tkinter that no one seems to explain. I am using the
from tinker import *
and when I run the program it errors out on the
window = tk()
i have tried to install tkinter but it tells me there is a new version of pip and tells me to run python.exe -m pip install --upgrade pip in the cmd prompt. this does nothing as it errors out in
ERROR: Could not install packages due to an OSError: [WinError 5] Access is denied: 'i:\\python\\lib\\site-packages\\pip-22.3.1.dist-info\\entry_points.txt'
Consider using the `--user` option or check the permissions.
what am I doing wrong? please help
I have tried everything that all the web sites have explained to no avail. and a simple
from tkinter import *
window = tk()
still errors out. :(
no window pops up
Try this and see if it works as expected - I've fixed a few things based on the information you've given and the comments from other users.
import tkinter as tk # star imports are best avoided
# notice the 'tk.' namespace prefix here - this is the way to go!
window = tk.Tk() # instantiate Tk
label = tk.Label(window, text='Hello, World!') # add a widget
label.pack() # add the widget to the window
window.mainloop() # start the app
Barring that, try running this command:
python.exe -m tkinter
It should bring up a basic example tkinter app. If that works, you'll know that tkinter is at least installed correctly on your machine.

why I can't use tkinter module with batch file

Batch code:
TITLE %~nx0
python "c:\users\aaa\desktop\coding\python\first project with tkinter\buttomtest.py"
pause
and python code is:
from tkinter import *
window = Label(text="gg")
print("tt")
and this is what I get:
It shows the cmd prompt window but it doesn't show the window of tkinter
Tkinter is a python module meant ONLY for python.
Now I see what you tried to do but learning python may be the way there is an OS module import OS which could allow you to run command prompt commands. os.system("command here") will allow you to run commands from the system DOS.
Hope this helps and enjoy the community!
You may also run a start command
start File.py
Exit
way to fix that: window.mainloop()
and there is no need for import OS

Are there settings in Jupyter notebooks to make Tkinter GUI work?

I'm learning to use Tkinter for another program I'm writing. This code works in Spyder and on a friends Jupyter notebook, but it does't work on mine. Is there something in the settings of my Jupyter notebook I need to change to make it work?
This is the error message I get:
TclError: no display name and no $DISPLAY environment variable
from tkinter import *
from tkinter.ttk import *
window = Tk()
window.title("Hello World")
window.geometry('1500x1500')
window.mainloop()

Issue with tkinter importing on PyCharm (Python 3.6.6)

I'm trying to use tkinter for the first time in python 3.6.6, using PyCharm. After importing tkinter, I wasn't able to use the Tk() function to create a window, but instead got an inspection stating that "Tk()" is an unresolved reference. When I run my short program, it also gives me name error. Can someone please tell me what the issue could be?
from tkinter import *
root = Tk()
root.mainloop()
Try running the following code:
import tkinter as tk
root = tk.Tk()
root.mainloop()

tkinter in Spyder

I am trying my first steps in tkinter. I use Spyder as IDE in Python 3.5.1 |Anaconda 4.0.0.
I want to run the very simple script below but it always crashes my Spyder. In a normal shell/bash it runs though and opens the canvas.
import tkinter as tkr
tk = tkr.Tk()
canvas = tkr.Canvas(tk, width=500, height=500)
canvas.grid()
tk.mainloop()
Under Preferences for the Ipython Console I already tried different settings (i.e. Qt, Automatik, Tkinter) but none of it did help.
What am I doing wrong (and how can I do it better)?
many thanks in advance
update to Spyder 3.0.1
https://pythonhosted.org/spyder/
https://github.com/spyder-ide/spyder/releases/tag/v3.0.1
I just did this on win 10: no crash, got blank "tk" separate window
You should try to change the graphics backend
Go to tools/preferences/I-python Console/Graphics and in backend change it to Tkinter.
That should do it!!
In the Spider menu bar, go to: Tools > Preferences
A window will open, then on left side go to: Completion and linting
In the right side, go to: introspection and below you will see different modules available in your current spider
In the section "Preload the following modules...", add tkinter to the end of the list
I found this suggestion while trying to get tkinter to display a GIF image in a label. First, on a Mac, the Tools menu item does not contain Preferences--look under the "python" menu instead. Even so, I have the
IPython Console Graphics backend set to Tkinter and tkinter is added to Completion and Linting. But all I get when launching from Spyder is an empty frame entitled "tk #442", whereas when I launch from the command line I get a frame with a nice GIF image as expected.

Categories