【问题标题】:Python tkinter Button not respondingPython tkinter 按钮没有响应
【发布时间】:2017-07-04 14:46:32
【问题描述】:

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,但我不能点击按钮,因为它不会响应。

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow,请花点时间通过 welcome tour 了解您在此处的方式(并获得您的第一个徽章),阅读如何创建 Minimal, Complete, and Verifiable example并检查How to Ask Good Questions,这样您就有机会获得反馈和有用的答案。
  • 第二个按钮永远不会出现,因为你没有 pack() 它。第一个按钮出现,但单击时不调用函数,因此“不响应”。

标签: python python-3.x tkinter turtle-graphics


【解决方案1】:

如 cmets 中所述,您似乎创建了两次相同的按钮,一次是在它未连接到函数但打包 em>,一旦它连接到一个函数但没有打包。如果将两者结合起来,您将获得一个工作按钮。

但是让我们在另一个问题开始之前先解决它——你调用了:

t = turtle.Pen()

不,当您在 tkinter 中工作时不会。如果你使用的是独立的 turtle,它是 Turtle/Pen 和 Screen,但如果你在 tkinter 中工作,它是 RawTurtle/RawPen 和 TurtleScreen。否则,您最终会遇到额外的窗口和潜在的根冲突。

将以上所有内容结合在一起,添加一个滚动画布供海龟在其上播放,并将控制台的print() 更改为滚动画布的turtle.write(),我们得到:

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

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    相关资源
    最近更新 更多