微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

主 GUI TK 中的海龟圆形

如何解决主 GUI TK 中的海龟圆形

首先,感谢您访问我的帖子。我是一名业余 Python 作家。现在,我正在编写一个代码来控制一些 Agilent 仪器,在我的 GUI 中我需要显示一个虚拟 LED,可以向操作员显示当前的测试进度状态,例如,黄色 = 测试,绿色 = 通过,红色 =失败。

我发现turtle模块可以通过添加turtle.shape('cicle')轻松创建一个圆,请看下面我有一个turtle代码

我想说清楚,我没有写下面的代码,我在这个网页上找到了代码

    from turtle import Screen,Turtle

    CURSOR_SIZE = 30

    def blink():
        pen,fill = turtle.color()
        turtle.color(fill,pen)
        screen.ontimer(blink,500)  # 1/4 second blink

    screen = Screen()

    turtle = Turtle()
    turtle.hideturtle()
    turtle.shape('circle')
    turtle.shapesize(50 / CURSOR_SIZE)
    turtle.color('red','Yellow')
    turtle.showturtle()

    blink()

所以,我想把按钮放到我的主 GUI 窗口中:

    ########## Main Window Design##########
    main = Tk()
    main.geometry('860x500+65+0')
    main.title('Firmware Upgrade System 1.0')
    Label(main,text='Firmware Upgrade System 1.0',font=("Tahoma",20)).place(x=0,y=0)


    ##! UUT 1 ####################################
    LabelFrame(main,text=' UUT 1 informatION : ',10),height=120,width=420,bd=2,relief='groove' ).place(x=10,y=40)
    LabelFrame(main,text='Serial Number',height=47,width=110,bd=3,relief='ridge').place(x=13,y=60)
    LabelFrame(main,text='Firmware Version',width=119,relief='ridge').place(x=126,y=60)

    def fw_upgrade():
        print("pas")
        blink()

    #######Buttons and Functions
    Button(main,text="FIRMWARE UPGRADE",12),height=1,width=20,command=fw_upgrade).place(x=450,y=440)

因此,每次我执行代码时,它都会打开一个带有虚拟 LED 的辅助窗口。如何合并到我的主 GUI 中?

感谢您的帮助...

解决方法

常规的 TurtleScreen 总是会创建一个新窗口。

海龟模块有另一个 RawTurtle 类,其工作方式与 Turtle 完全相同,但利用现有的 tk.Canvasturtle.TurtleScreenturtle.Screen 实例,你必须提供。 Take a look at the turtle docs,they have lots of examples too

您还应该在代码末尾的 mainloop() 窗口上调用 Tk(),以便窗口保持打开状态。 (如果您以后可能会更改它们的任何属性或文本,那么将 LabelFrames 和所有小部件的实例存储在变量中也是一个好主意)

from tkinter import Tk,Label,LabelFrame,Button,Canvas
from turtle import TurtleScreen,RawTurtle

def fw_upgrade():
    print("pas")

########## Main Window Design##########
main = Tk()
main.geometry('860x500+65+0')
main.title('Firmware Upgrade System 1.0')
label1 = Label(main,text='Firmware Upgrade System 1.0',font=("Tahoma",20))
label1.place(x=0,y=0)
##! UUT 1 ####################################
frame1 = LabelFrame(main,text=' UUT 1 INFORMATION : ',10),height=120,width=420,bd=2,relief='groove' )
frame1.place(x=10,y=40)
frame2 = LabelFrame(main,text='Serial Number',height=47,width=110,bd=3,relief='ridge')
frame2.place(x=13,y=60)
frame3 = LabelFrame(main,text='Firmware Version',width=119,relief='ridge')
frame3.place(x=126,y=60)
btn = Button(main,text="FIRMWARE UPGRADE",12),height=1,width=20,command=fw_upgrade)
btn.place(x=450,y=440)

canv = Canvas(main)
turtlescr = TurtleScreen(canv)
canv.place(x=0,y=100)

CURSOR_SIZE = 30
turtle = RawTurtle(turtlescr)
turtle.hideturtle()
turtle.shape('circle')
turtle.shapesize(50 / CURSOR_SIZE)
turtle.color('red','Yellow')
turtle.showturtle()

main.mainloop()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。