Python tkinter 按钮没有响应
Python tkinter Button not responding
TKINTER 图形
from tkinter import *
tk = Tk()
btn = Button(tk, text="Click Me") #Makes a useless button
btn.pack() #Shows the button
import turtle #Makes the graphics work
t = turtle.Pen()
def hello(): #Makes button not
print('hello here')
btn = Button(tk, text="Click Me", command=hello)
当我点击按钮时,程序应该说 hello there
,但我不能点击按钮,因为它没有响应。
如评论中所述,您似乎创建了两次相同的按钮,一次是 未连接 到一个函数但 packed,一旦它 connected 到一个函数但是未打包。如果将两者结合起来,您将获得一个可用的按钮。
但让我们在问题开始之前进入并解决另一个问题——您调用了:
t = turtle.Pen()
不,当你在 tkinter 中工作时不会。如果您独立使用 turtle,则为 Turtle/Pen 和 Screen,但如果您在 tkinter 中工作,则为 RawTurtle/RawPen 和 TurtleScreen。否则你最终会遇到额外的 windows 和潜在的根冲突。
将以上所有组合在一起,添加一个滚动的 canvas 供乌龟玩耍,并将控制台的 print()
更改为 turtle.write()
滚动的 canvas,我们得到:
import tkinter as tk
import turtle # Makes the graphics work
def hello(): # Makes button not
turtle.write('hello there', align='center', font=('Arial', 18, 'normal'))
root = tk.Tk()
button = tk.Button(root, text="Click Me", command=hello) # Makes a button
button.pack() # Shows the button
canvas = turtle.ScrolledCanvas(root)
canvas.pack(side=tk.LEFT)
screen = turtle.TurtleScreen(canvas)
turtle = turtle.RawPen(screen, visible=False)
screen.mainloop()
TKINTER 图形
from tkinter import *
tk = Tk()
btn = Button(tk, text="Click Me") #Makes a useless button
btn.pack() #Shows the button
import turtle #Makes the graphics work
t = turtle.Pen()
def hello(): #Makes button not
print('hello here')
btn = Button(tk, text="Click Me", command=hello)
当我点击按钮时,程序应该说 hello there
,但我不能点击按钮,因为它没有响应。
如评论中所述,您似乎创建了两次相同的按钮,一次是 未连接 到一个函数但 packed,一旦它 connected 到一个函数但是未打包。如果将两者结合起来,您将获得一个可用的按钮。
但让我们在问题开始之前进入并解决另一个问题——您调用了:
t = turtle.Pen()
不,当你在 tkinter 中工作时不会。如果您独立使用 turtle,则为 Turtle/Pen 和 Screen,但如果您在 tkinter 中工作,则为 RawTurtle/RawPen 和 TurtleScreen。否则你最终会遇到额外的 windows 和潜在的根冲突。
将以上所有组合在一起,添加一个滚动的 canvas 供乌龟玩耍,并将控制台的 print()
更改为 turtle.write()
滚动的 canvas,我们得到:
import tkinter as tk
import turtle # Makes the graphics work
def hello(): # Makes button not
turtle.write('hello there', align='center', font=('Arial', 18, 'normal'))
root = tk.Tk()
button = tk.Button(root, text="Click Me", command=hello) # Makes a button
button.pack() # Shows the button
canvas = turtle.ScrolledCanvas(root)
canvas.pack(side=tk.LEFT)
screen = turtle.TurtleScreen(canvas)
turtle = turtle.RawPen(screen, visible=False)
screen.mainloop()