Python 导入tkinter或root.tkinter对我不起作用

Python 导入tkinter或root.tkinter对我不起作用,python,tkinter,Python,Tkinter,我最近开始学习Python 3,到目前为止我一直很喜欢它 我正在尝试创建一个哈利波特故事游戏作为一个项目,为此我正在学习如何使用Tkinter 我对如何导入它做了一些研究,但似乎没有任何效果 我不知道哪部分出错,所以我添加了整个代码 我找了很多地方,问了一些朋友,但没有一个可行的解决方案适合我 from tkinter import * as tk import tkinter as tk root = tk.Tk() #Initialize main window window = tkint

我最近开始学习Python 3,到目前为止我一直很喜欢它

我正在尝试创建一个哈利波特故事游戏作为一个项目,为此我正在学习如何使用Tkinter

我对如何导入它做了一些研究,但似乎没有任何效果

我不知道哪部分出错,所以我添加了整个代码

我找了很多地方,问了一些朋友,但没有一个可行的解决方案适合我

from tkinter import * as tk
import tkinter as tk
root = tk.Tk()
#Initialize main window
window = tkinter.Tk()
#Size window properly
window.minsize (1280, 720)
#Now to give your window a title
window.title ("HogWarts Express")
#Add an Icon
root.iconbitmap("/Users/name/Desktop/The_Hogwarts_Experience/Harry_Potter_Story_Game/icon.ico")
tk.call("wm", "iconphoto", )
root.iconphoto

#Place Label
label = tkinter.Label (window, text="Continue")

#Place the label and specific coordinates
label.place (x=600, y=330)

#Initialize the counter
counter = 0

#Function to find the screen dimesions & calculate the center and set geometry
def center_window(w=600, h=700):
    # Call all pending idle tasks - carry out geometry management and redraw widgets.
    win.update_idletasks()
    # Get width and height of the screen
    ws = root.winfo_screenwidth()
    hs = root.winfo_screenheight()
    # Calculate x, y
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    # Set geometry
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))



#Define button press function
def press():
    #use global counter variable.
    global counter
    #count each button's press
    counter = counter + 1
    #set Label
    label.config(text=f"Button clicked: {counter} times")

#Place button on the window
button = tkinter.Button (window, text="Click Me", command=press)

#place at coordinates
button.place(x=10, y=40)

#show the window
window.mainloop()


这段代码中有几个问题,解决方法如下:

#from tkinter import * as tk #this is wrong in different ways, dont do that
import tkinter as tk
root = tk.Tk()
#window = tkinter.Tk() # you alreade have tk.Tk()
root.minsize(1280, 720) #use root since root = tk.Tk()
root.title("HogWarts Express")# dont use space here
root.iconbitmap("/Users/name/Desktop/The_Hogwarts_Experience/Harry_Potter_Story_Game/icon.ico")
#tk.call("wm", "iconphoto", ) 
#root.iconphoto

label = tk.Label(root, text="Continue") #imported as tk so use tk
label.place(x=600, y=330)

counter = 0

def center_window(w=600, h=700):
    root.update_idletasks() #root is the window
    ws = root.winfo_screenwidth()
    hs = root.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))


def press():
    global counter
    counter = counter + 1
    label.config(text=f"Button clicked: {counter} times")

button = tk.Button(root, text="Click Me", command=press) #root is the window/master
button.place(x=10, y=40)

root.mainloop()

我强烈建议您遵循一个教程,但不多,它令人困惑。我已经开始了。我在youtube上学习了这段代码的教程,所以它和哈利波特没有任何关系。我只是在试验一些东西。
来自tkinter import*as tk
:这是不正确的。移除它。如果在此之后您仍然有问题,请添加问题的回溯。代码中存在一些与导入无关的问题,例如,您编写了
tkinter.Label
,但模块tkinter不存在。你把它命名为
tk
。非常感谢!我被所有关于Python的可用信息弄得不知所措,不知道从哪里开始。我感谢你的链接和更正!谢谢你@科马斯我也有同样的问题,一个接一个的理解是关键。请注意它已经完成了!那个播放列表非常有用!非常感谢你!