如何在 python 的 turtle 模块中制作多个按钮而不用硬核
How to make multiple buttons in python's turtle module without hardcore
我想做一个带按钮的乌龟python 'app'。我知道如何制作一个按钮,但我想在那里制作 1 个以上的按钮并且没有任何大的 HARDCORE。
我在 YouTube 上看到了说明并阅读了评论,有人问了这样的问题,YTBer 回答说“是的”。你需要一些铁杆的东西。我不要硬核。
...你是对的,当使用 2 个按钮时它不起作用,我在制作视频后意识到但幸运的是我找到了解决方案......所以它不起作用的原因不起作用是因为两个按钮需要 turtle.onscreenclick 方法来监听两个对象的方法,但由于某种原因它不能这样做,所以我们可以通过以下方式在 class 中硬编码它:
import turtle
import time
wn = turtle.Screen()
wn.bgcolor("Green")
wn.setup(700, 700)
wn.tracer(0)
class Button(turtle.Turtle):
def __init__(self,s, text, x, y, w, h, c, i, p, a=None, b2=None):
turtle.Turtle.__init__(self)
self.msg = text
self.x = x
self.y = y
self.pens...
我尝试执行() cdlane 的代码,但我得到了
I got a
Traceback (most recent call last):
File "<pyshell#2>", line 60, in <module>
''')
File "<string>", line 51, in <module>
File "<string>", line 10, in __init__
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 3816, in __init__
visible=visible)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 2557, in __init__
self._update()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 2660, in _update
self._update_data()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 2646, in _update_data
self.screen._incrementudc()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 1292, in _incrementudc
raise Terminator
turtle.Terminator
使用了 exec('') 命令
turtle
建立在 tkinter
和 tkinter.Canvas
之上,因此您可以使用 tkinter.Button
和其他小部件并添加到 canvas.
import turtle
import tkinter as tk
def test1():
print("clicked World")
def test2():
print("clicked World")
canvas = turtle.getcanvas()
parent = canvas.master
button1 = tk.Button(parent, text='Hello', command=test1)
id1 = canvas.create_window((0,0), window=button1)
button2 = tk.Button(parent, text='World', command=test2)
id2 = canvas.create_window((100,0), window=button2)
turtle.done()
Button
需要父级 - 这里只能是 canvas.master
。如果你忘记了 parent 那么它会创建 tkinter 的 window 并且你会看到两个 windows。
command=
期望没有 ()
和参数的函数名称。单击按钮时它会运行此功能。
Button
还有其他选项,例如 background
、font
等
create_window
用于添加任何 tkinter 的小部件。第一个参数是它在 canvas 上的位置。它还有其他选项。
crete_window
提供 ID,您可以使用该 ID 删除小部件 canvas.delete(ID)
或移动它 canvas.move(ID, offset_x, offset_y)
Tkinter:Canvas, Button, more widgets
编辑: 它创建了 10 个按钮,每个按钮使用 lambda
来分配带有参数的功能。每个按钮都会更改 Label
上的文本
import turtle
import tkinter as tk
def test(value):
print("clicked", value)
label['text'] = str(value)
canvas = turtle.getcanvas()
parent = canvas.master
label = tk.Label(parent, text="???")
canvas.create_window((0, -30), window=label)
for x in range(10):
text = 'Button {}'.format(x)
button = tk.Button(parent, text=text, command=(lambda val=x:test(val)))
canvas.create_window((0, x*30), window=button)
turtle.done()
我想做一个带按钮的乌龟python 'app'。我知道如何制作一个按钮,但我想在那里制作 1 个以上的按钮并且没有任何大的 HARDCORE。
我在 YouTube 上看到了说明并阅读了评论,有人问了这样的问题,YTBer 回答说“是的”。你需要一些铁杆的东西。我不要硬核。
...你是对的,当使用 2 个按钮时它不起作用,我在制作视频后意识到但幸运的是我找到了解决方案......所以它不起作用的原因不起作用是因为两个按钮需要 turtle.onscreenclick 方法来监听两个对象的方法,但由于某种原因它不能这样做,所以我们可以通过以下方式在 class 中硬编码它:
import turtle
import time
wn = turtle.Screen()
wn.bgcolor("Green")
wn.setup(700, 700)
wn.tracer(0)
class Button(turtle.Turtle):
def __init__(self,s, text, x, y, w, h, c, i, p, a=None, b2=None):
turtle.Turtle.__init__(self)
self.msg = text
self.x = x
self.y = y
self.pens...
我尝试执行() cdlane 的代码,但我得到了
I got a
Traceback (most recent call last):
File "<pyshell#2>", line 60, in <module>
''')
File "<string>", line 51, in <module>
File "<string>", line 10, in __init__
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 3816, in __init__
visible=visible)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 2557, in __init__
self._update()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 2660, in _update
self._update_data()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 2646, in _update_data
self.screen._incrementudc()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 1292, in _incrementudc
raise Terminator
turtle.Terminator
使用了 exec('') 命令
turtle
建立在 tkinter
和 tkinter.Canvas
之上,因此您可以使用 tkinter.Button
和其他小部件并添加到 canvas.
import turtle
import tkinter as tk
def test1():
print("clicked World")
def test2():
print("clicked World")
canvas = turtle.getcanvas()
parent = canvas.master
button1 = tk.Button(parent, text='Hello', command=test1)
id1 = canvas.create_window((0,0), window=button1)
button2 = tk.Button(parent, text='World', command=test2)
id2 = canvas.create_window((100,0), window=button2)
turtle.done()
Button
需要父级 - 这里只能是 canvas.master
。如果你忘记了 parent 那么它会创建 tkinter 的 window 并且你会看到两个 windows。
command=
期望没有 ()
和参数的函数名称。单击按钮时它会运行此功能。
Button
还有其他选项,例如 background
、font
等
create_window
用于添加任何 tkinter 的小部件。第一个参数是它在 canvas 上的位置。它还有其他选项。
crete_window
提供 ID,您可以使用该 ID 删除小部件 canvas.delete(ID)
或移动它 canvas.move(ID, offset_x, offset_y)
Tkinter:Canvas, Button, more widgets
编辑: 它创建了 10 个按钮,每个按钮使用 lambda
来分配带有参数的功能。每个按钮都会更改 Label
import turtle
import tkinter as tk
def test(value):
print("clicked", value)
label['text'] = str(value)
canvas = turtle.getcanvas()
parent = canvas.master
label = tk.Label(parent, text="???")
canvas.create_window((0, -30), window=label)
for x in range(10):
text = 'Button {}'.format(x)
button = tk.Button(parent, text=text, command=(lambda val=x:test(val)))
canvas.create_window((0, x*30), window=button)
turtle.done()