如何在标签中的 tkinter 上制作字幕? [英] How to make marquee on tkinter in label?

查看:35
本文介绍了如何在标签中的 tkinter 上制作字幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个源代码:

from Tkinter import *
import tkMessageBox
import time
class Window(Tk):
    def __init__(self,parent):
        Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()


    def initialize(self):
        self.geometry('450x250+0+0')
        self.configure(background="#379DDB")
        self.title('Konversi Bilangan')
        **self.label = Label(self, text='Konversi Dari',font=('times',24,'italic'),bg="#379DDB")**
        self.label.pack()
        self.tombol=Button(self, text='Biner',font=(18),borderwidth='3px',width=10,command=self.OnButtonClick1,bg="#69ABD3")
        self.tombol.pack(side=TOP)
        self.tombol2=Button(self, text='Desimal',font=(18),borderwidth='3px' ,width=10, command=self.OnButtonClick2,bg="#69ABD3")
        self.tombol2.pack(side=TOP)
        self.tombol3=Button(self, text='Oktal',font=(18),borderwidth='3px' ,width=10,command=self.OnButtonClick3,bg="#69ABD3")
        self.tombol3.pack()
        self.tombol4=Button(self, text='Hexa',font=(18),borderwidth='3px' ,width=10,command=self.OnButtonClick4,bg="#69ABD3")
        self.tombol4.pack()
        self.tombol5=Button(self,text='Quit',font=(18),borderwidth='3px' ,width=10, fg='red', command= self.quit,bg="#69ABD3")
        self.tombol5.pack()

如何从我的粗体中制作选框?如果不可能在 tkinter 中如何制作像 vb 这样的字幕?

How to make marquee from that I bold? If it is not possible how to make marquee like vb in tkinter?

推荐答案

这将很难集成,因为 tkinter 不会玩得很好"无限循环.

This is going to be hard to integrate because tkinter doesn't "play nice" with infinite loops.

下面的程序 在此处的帮助下编写 创建了一个选取框,这只是证明可以做到的一个示例,并且是一种糟糕的方法.

The below program written with assistance from here creates a marquee, this is simply an example to prove this can be done and is a poor way of doing this.

from tkinter import *

root = Tk()

text="Lorem Ipsum"

text = (' '*20) + text + (' '*20)

marquee = Text(root, height=1, width=20)
marquee.pack()

i = 0

def command(x, i):
    marquee.insert("1.1", x)
    if i == len(text):
        i = 0
    else:
        i = i+1
    root.after(100, lambda:command(text[i:i+20], i))

button = Button(root, text="Start", command=lambda: command(text[i:i+20], i))
button.pack()

root.mainloop()

这也使用了 Text 小部件而不是 Label 小部件,主要是因为这样做更简单.

This also uses the Text widget instead of the Label widget, mostly because it's simpler to do this way.

要在程序启动时启动它,只需将其调整为以下内容:

To start it on program launch, simply adjust it to the below:

from tkinter import *

root = Tk()

text="Lorem Ipsum"

text = (' '*20) + text + (' '*20)

marquee = Text(root, height=1, width=20)
marquee.pack()

i = 0

def command(x, i):
    marquee.insert("1.1", x)
    if i == len(text):
        i = 0
    else:
        i = i+1
    root.after(100, lambda:command(text[i:i+20], i))

command(text[i:i+20], i)

root.mainloop()

这篇关于如何在标签中的 tkinter 上制作字幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆