Why isn't the tkinter button showing up on the screen? - python

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.
I have already tried removing turtle.mainloop() but that doesn't work either.
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. 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()
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

Related

How to change the background color using tkinter.Label

I'm trying to build GUI program and want to know who to change the label background color?
import tkinter as tk
window = Tk()
timer_label = Label(text="Timer", fg=GREEN, font=(FONT_NAME, 50, "normal"))
timer_label.pack()
root.mainloop()
To change the background you can use the "bg" for expmle:
timer_label = Label(text="Timer", fg=GREEN, bg="red" font=(FONT_NAME, 50, "normal"))

Tkinter get width of button made with `create_window()`

I'm using Tkinter combined with Python Turtle Graphics and I want to be able to get the width of a button made using create_window().
I am able to set the value using the code below, but I want to get the value rather than set it. How do I do this please?
import turtle
import tkinter as tk
screen = turtle.Screen()
canvas = screen.getcanvas()
button = tk.Button(canvas.master, text="Press me")
w1 = canvas.create_window(0, 0, window=button)
canvas.itemconfig(w1, width=100)
turtle.done()
This will do it.
import turtle
import tkinter as tk
screen = turtle.Screen()
canvas = screen.getcanvas()
button = tk.Button(canvas.master, text="Press me")
w1 = canvas.create_window(0, 0, window=button)
canvas.itemconfig(w1, width=100)
button.update()
print(button.winfo_width())
print(button.winfo_height())
turtle.done()
Make sure to update before calling .winfo_width(). :-)

How to speed up the turtle while in a tkinter canvas

I'm trying to make the turtle move faster, which I would normally do using
import turtle as t
t.speed(0)
t.tracer(0,0)
But when I have it in a canvas using RawTurtle(), I'm not sure how to do this.
root = tk.Tk() #create root window
#create turtle canvas
canvas = tk.Canvas(root,width=500,height=500)
canvas.pack()
t = turtle.RawTurtle(canvas)
t.ht()
Here's my code. Anyone know how?
First, either do one or the other of these, not both:
t.speed(0)
t.tracer(0,0)
The speed(0) aka speed('fastest') gets you the fastest drawing animation. The tracer(0) eliminates drawing animation altogether. They don't add together.
You can get maximum speed, i.e. eliminate drawing animation, in turtle embedded in a Canvas by using turtle's TurtleScreen() wrapper:
import tkinter as tk
from turtle import TurtleScreen, RawTurtle
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack()
screen = TurtleScreen(canvas)
screen.tracer(False)
turtle = RawTurtle(screen)
turtle.circle(100)
screen.tracer(True)
screen.mainloop()
I made a few edits to your code. For starters, you need to work all the commands after the t = turtle.RawTurtle(canvas) line. Then you need to add a .mainloop() function at the end.
This would be your final code:
import tkinter as tk
root = tk.Tk() #create root window
import turtle
#create turtle canvas
canvas = tk.Canvas(root,width=500,height=500)
canvas.pack()
t = turtle.RawTurtle(canvas)
t.speed(1)
t.forward(100)
t.ht()
root.mainloop()
When the speed is set to 1, this is the output:
When the speed is set to 0, this is the output:
Sorry about the gifs, and hope this helps!

Trying to take user input and make a turtle dot

Trying to take user input in this case a numbers and make a turtle make a dot with the number that we got from the user input. It is for a school project that I am trying to do and I tried finding YouTube videos they really did not help.
from tkinter import *
import tkinter
import turtle
wn = turtle.Screen()
wn.bgcolor('black')
player = turtle.Turtle()
player.shape('turtle')
player.color('white')
def sad():
player.dot(str(kj.get()))
top = tkinter.Tk()
top.geometry('600x600')
kj = Entry(top, bd =5)
kj.pack()
B = tkinter.Button(top, text ="Hello", command = sad)
B.pack()
wn.mainloop()
top.mainloop()
When using Python turtle in conjunction with tkinter, you need to use the embedded turtle methods, not the standalone methods you used. As turtle is built atop tkinter, you've effectively created two roots and will eventually run into trouble. (E.g. images probably won't work for you.) You're clearly confused by the combination as you call both top.mainloop() and wn.mainloop()!
Here's an example of how to embed turtle in tkinter for your program:
import tkinter as tk
from turtle import TurtleScreen, RawTurtle
def set_position():
player.setposition(x_entry.get(), y_entry.get())
player.dot(30, 'blue')
player.home()
top = tk.Tk()
canvas = tk.Canvas(top, width=600, height=600)
canvas.pack()
screen = TurtleScreen(canvas)
screen.bgcolor('black')
player = RawTurtle(screen)
player.shape('turtle')
player.color('red', 'white')
player.penup()
x_entry = tk.DoubleVar()
tk.Label(top, text="X: ").pack(side=tk.LEFT)
tk.Entry(top, textvariable=x_entry).pack(side=tk.LEFT)
y_entry = tk.DoubleVar()
tk.Label(top, text="Y: ").pack(side=tk.LEFT)
tk.Entry(top, textvariable=y_entry).pack(side=tk.LEFT)
tk.Button(top, text="Draw Dot!", command=set_position).pack()
screen.mainloop()
My recommendations are: first, try to work completely within standalone turtle if you can, and not introduce tkinter unless you really need to; second, don't trust any answer that makes this same mistake of trying to use standalone turtle classes embedded in a tkinter environment.
with turtle you can tell it to go to a position using setPos commands. If you just convert the values from your user input into coordinates, tell you turtle to go there and then start drawing.
Here is a solution:
import turtle
from time import sleep
from tkinter import *
#Setup
root=Tk()
wn = turtle.Screen()
wn.bgcolor('black')
player = turtle.Turtle()
player.shape('turtle')
player.color('white')
def goToLocation(coords):
#Get user input and split it into two different coordinants (coordsx and coordsy)
coordsx, coordsy = coords.split(" ")
#Set turtles position to the coords specified
player.hideturtle()
player.penup()
player.setpos(int(coordsx),int(coordsy))
#Draw the circle of size
player.pensize(50)
player.showturtle()
player.pendown()
player.forward(1)
sleep(5)
#Button clicked handler
def retrieve_input():
inputValue=textBox.get("1.0","end-1c")
print(inputValue)
#Calls the previous function
goToLocation(inputValue)
#Input box setup
textBox=Text(root, height=2, width=10)
textBox.pack()
buttonCommit=Button(root, height=1, width=10, text="Commit",
command=lambda: retrieve_input())
#command=lambda: retrieve_input() >>> just means do this when i press the button
buttonCommit.pack()
mainloop()

Can't display my turtle object in my Tkinter canvas

I'm very new to Python. I wanted to display a turtle object on top of a canvas in Tkinter. I'm not sure why the object is not displaying.
I used the RawTurtle() in order to use the Tkinter canvas as screen.
root =tk.Tk()
outercanvas = Canvas(root, width=900, height=800, bg='#00ffff')
outercanvas.pack(expand=Y,fill=BOTH)
innercanvas = Canvas(outercanvas, width=680, height=700)
outercanvas.create_window(100, 40, anchor=NW, window=innercanvas)
bg = tk.PhotoImage(file="level2.png")
innercanvas.create_image(-5, 0, image = bg, anchor=NW)
bob = turtle.RawTurtle(innercanvas)
I expect the turtle to appear on top of the canvas, so that I can manipulate it later.
What am I missing?
Your background image and the turtle appear to be in conflict. Try it this way instead:
import tkinter as tk
from turtle import RawTurtle, TurtleScreen
root = tk.Tk()
outercanvas = tk.Canvas(root, width=900, height=800, bg='#00ffff')
outercanvas.pack(expand=tk.Y, fill=tk.BOTH)
innercanvas = tk.Canvas(outercanvas, width=680, height=700)
outercanvas.create_window(100, 40, anchor=tk.NW, window=innercanvas)
screen = TurtleScreen(innercanvas)
screen.bgpic("level2.gif")
bob = RawTurtle(screen)
bob.circle(100)
screen.mainloop()
Note that I had to make and use "level2.gif" for this to work but you may have a newer underlying tkinter, and turtle, that accepts PNG files.

Categories