tkinter in python not working and no web fix works - python

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.

Related

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.

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

Event listener without GUI

I need an event listener that starts and quits my script whenever I press a key. So this is a script that lets me know if a key has been pressed:
import Tkinter as tk
def onKeyPress(event):
text.insert('end', 'You pressed %s\n' % (event.char, ))
root = tk.Tk()
root.geometry('300x200')
text = tk.Text(root, background='black', foreground='white', font=('Comic Sans MS', 12))
text.pack()
root.bind('<KeyPress>', onKeyPress)
root.mainloop()
That works great but the problem is that I don't need a GUI. I should be able to press a key wherever I want. How would that be possible?
Like Rawing said, you can use the keyboard library, for example:
import keyboard
keyboard.add_hotkey('a', lambda: print "a was pressed")
Note that for this to work, the keyboard library must be installed, you can do this with $ sudo pip install keyboard
Edit: You might have to use $ sudo python -m pip install keyboard
Edit: Or you might have to use $ sudo py -m pip install keyboard
Edit: Or even $ sudo -H pip install keyboard
Also, note that I'm assuming you're using python 2.7.
Edit: if you wonder why I assumed you were using python 2.7 it was because you used from Tkinter import * in your GUI example, and in python 2.7, the tkinter module is called "Tkinter", whilst in python 3.6, tkinter is called "tkinter" so in python 3 you should use from tkinter import * (Note the lowercase "t" versus the capital "T" in tkinter/Tkinter)
Also, note that the keyboard module doesn't work on Mac (at least their pip page only says they support Windows and Linux, but it might work if you have luck)
Lastly, note that I haven't tried this myself since I currently do not have access to a computer with python installed. If it doesn't work, comment me and I'll try to find another solution :)
Edit: #RezaSaadati approved that it worked.

Tkinter for Windows7-64Bit

Can you please let me know how to download tkinter for windows 7 64bit. I am getting an error while i do it:
from Tkinter import *)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
from Tkinter import *
ImportError: No module named 'Tkinter'
If you are using python 3 then the code would use a lower case tkinter.
#python 2
from Tkinter import *
#python 3
from tkinter import *
if that doesn't fix it then you can install it in several ways. check out this page: http://www.tkdocs.com/tutorial/install.html
If you can't get that to work another option is using pip. pip allows you to install modules easily. If you have the newest version if python 2 then it is installed and all you have to do is set a path. Click the start button and right click on computer. Select properties and then advanced settings. Under one of the tabs is a button called environment variables. Click on it and in the new windows scroll down until you see the word "path" on the right. Click on the text to the left of it and put in a ; to put in a new path. I don't know exactly how your file structure is but find python and set a path to the folder called scripts within python. An example would be something like this: "C:\python27\scripts".
There are two possible reasons that you are not able to get Tkinter working:
First Reason
First, this is dependent on if you have installed tk onto your computer. To find out if you have, and what version, run a Python shell via:
% C:\python32\python
From the Python command prompt, enter these two commands:
>>> import tkinter
>>> tkinter._test()
This should pop up a small window; the first line at the top of the window should say "This is Tcl/Tk version 8.5"; Or whatever version has been installed on your computer. If you haven't installed, you'll want to get tkinter on there. I've found the following that may help with that:
http://www.tkdocs.com/tutorial/install.html
Second Reason
If installing tkinter onto your machine isn't the issue, then this is most likely due to what version of python you are using, as this will change some small things.
Python 3:
from tkinter import *
Python 2.7:
from Tkinter import *
If you have python you are trying to run from 2.7 or before but are using it in Python 3, you may find the 2to3 tool helpful for adapting:
https://docs.python.org/2/glossary.html#term-to3
open IDLE Python GUI and type in import tkinter and after that type import_tkinter and if you wanna run a test just type in tkinter._test()
works for python 3.4.3

build cmd into Tkinter window

Hi i was wondering if you could have the comand prompt box pop into the Tkinter window when you start the program? Some thing like:
from Tkinter import *
admin = Tk()
cmd = Cmd(admin)
cmd.pack()
admin.mainloop()
I'm on windows
http://tkinter.unpythonic.net/wiki/CmdTkHere is what you want, this is not just opening a cmd window. its embedding cmd.exe into a Tkinter.Frame. And a note here if you rename the python script to ".pyw" extension, the console will be hidden. Except for in a virtual environment's.
I don't believe there is any built in console widget. It may be possible to whip up a custom one using the Tkinter Text widget. However, that would take a bit of effort/time.
Another possible option is simply have your program launch command prompt.
Two different ways to launch command prompt on a Windows machine.
import subprocess, os
subprocess.Popen('cmd.exe')
os.system("cmd.exe")
EDIT:
Unforunetly I don't believe there is any built in widget like that. However I thought of another possible solution, check out the code for the IDLE GUI, it has a console and the GUI portion is entirley written using Tkinter. So you may be able to utelize that code.

Categories