简体   繁体   中英

Tkinter install not working after pip install tk

I need Tkinter to use in one of my projects as a button etc. i pip installed Tkinter with the code below as i followed a tutorial online and it said to

 pip install tk

is this not correct as when I import Tkinter import Tkinter . it says module not found?

I have tried to restart my project and have tried every possible pip install combination ever.

Please help explain Tia.

If you are using Python 3, Tkinter is built in. But you have to use a lowercase letter. Don't use "Tkinter", use "tkinter" instead.

Also, don't use "import tkinter", use "from tkinter import *", to import everything.

Example:

from tkinter import *
def command():
  print("Hello, world!")
root = Tk()
root.title("tkWindow")
btn = Button(root, text="Click Me!", command=command)
btn.pack()
root.mainloop()

Also, remember that when you import everything from Tkinter (using from tkinter import *), you don't use tk.Tk() or tk.Button(), you just have to use Tk() and Button().

This should work for you. Good luck, and happy coding!

For me, it depended on what version of python I was using. When I was trying to do turtle graphics for the first time, my Tkinter module wasn't importing. After from tkinter import Tk , it worked. When we moved to python 3.8.5, it clearly had tkinter built into python 3 already, so try pip installing (just in case), then try from tkinter import Tk again. If it isnt working, check to make sure that you're on the latest version of python!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM