为什么tkinter按钮没有显示在屏幕上?

时间:2019-08-04 21:41:03

标签: python tkinter pygame turtle-graphics

我正在做一个游戏,屏幕上的第一件事是一个说玩游戏的按钮。但是由于某种原因按钮没有显示在屏幕上吗?函数play_sound_game基本上就是我其余的代码。

我已经尝试了删除turtle.mainloop(),但这也不起作用。

    import turtle
    import tkinter as tk
    import time
    import pygame

    screen = turtle.Screen()

    turtle.ht()
    screen.bgcolor("blue")
    turtle.color('deep pink')
    style = ('Courier', 80, 'italic')
    turtle.pu()
    turtle.goto(-318,176)
    turtle.pu
    turtle.write('RHYMING WORDS', font=style)
    turtle.hideturtle()


    turtle.mainloop()


    #Button for play game
    button_playgame = tk.Button(canvas.master, text="Play Game", command=play_sound_game, font=('Arial', '65',"bold"), foreground = 'red')

    button_playgame.config(height = -1, width = 4)
    canvas.create_window(272, 88, window=button_playgame)

我没有收到任何错误消息。

1 个答案:

答案 0 :(得分:1)

turtle使用模块Canvas中的小部件tkinter。要添加按钮,您必须有权访问此画布

canvas = screen.getcanvas()

然后您可以在

中使用它
tk.Button(canvas.master, ...)

canvas.create_window(...)

由于turtle.mainloop()一直运行到关闭窗口,因此您必须在mainloop()之前创建按钮

工作示例。

import turtle
import tkinter as tk

def play_sound_game():
    pass

screen = turtle.Screen()

turtle.ht()
screen.bgcolor("blue")
turtle.color('deep pink')
style = ('Courier', 80, 'italic')
turtle.pu()
turtle.goto(-318,176)
turtle.pu
turtle.write('RHYMING WORDS', font=style)
turtle.hideturtle()

canvas = screen.getcanvas()

button_playgame = tk.Button(canvas.master, text="Play Game", command=play_sound_game, font=('Arial', '65',"bold"), foreground='red')
#button_playgame.config(height=1, width=4)

canvas.create_window(272, 88, window=button_playgame)

turtle.mainloop()

在Linux上

enter image description here