简体   繁体   English

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

[英]Why isn't the tkinter button showing up on the screen?

I am making a game and the first thing on the screen is a button that says play game. 我正在做一个游戏,屏幕上的第一件事是一个说玩游戏的按钮。 But the button isn't showing up on the screen for some reason? 但是由于某种原因按钮没有显示在屏幕上吗? The function play_sound_game is basically the rest of my code. 函数play_sound_game基本上是我代码的其余部分。

I have already tried removing turtle.mainloop() but that doesn't work either. 我已经尝试过删除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)

I didn't get any error messages. 我没有收到任何错误消息。

turtle uses widget Canvas from module tkinter . turtle使用模块tkinter部件Canvas To add button you have to get access to this canvas 要添加按钮,您必须访问此画布

canvas = screen.getcanvas()

and then you can use it in 然后可以在其中使用

tk.Button(canvas.master, ...)

and

canvas.create_window(...)

Because turtle.mainloop() runs all time till you close window so you have to create button before mainloop() 因为turtle.mainloop()一直运行到关闭窗口,所以您必须在mainloop()之前创建按钮

Working example. 工作示例。

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

On Linux 在Linux上

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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