Issue with tkinter importing on PyCharm (Python 3.6.6) - python

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()

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.

Python and tkinter confusion

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.

Can a tkinter button's command execute root.mainloop() from another gui?

I have a tkinter python script, containing a second imported tkinter python script,
What I want to do is to run the root.mainloop() of the second tkinter python script which has been all imported, by clicking a button made in the first tkinter python script.
It is going to be implemented like this :
Button(root, command = Second GUI's root.mainloop())
Is this possible to be achieved ? if not, are there any alternatives available to open a 2nd GUI Window from a button created in the 1st GUI python script ?
Yes, you need to import it to your main script and then call the function from there.

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()

Categories