如何在标签上的tkinter制作选框?

时间:2017-11-10 13:37:13

标签: python 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一样的字幕?

3 个答案:

答案 0 :(得分:0)

这很难整合,因为tkinter没有"玩得很好" 有无限循环。

以下程序written with assistance from here创建了一个选取框,这只是一个证明可以完成此操作的示例,并且这是一种不好的方法。

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小部件,主要是因为这样做更简单。

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

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

答案 1 :(得分:0)

在tkinter中进行动画的模式是使用after一次安排一帧动画。它看起来像这样,其中fps被定义为每秒所需的帧数,widget是一个tkinter小部件:

def animate():
    <draw one frame of animation>
    widget.after(int(1000/fps), animate)

对于选框,最简单的解决方案之一是使用画布,因为它有一个方便的move方法,可用于从右向左移动文本。

以下是一个例子:

import tkinter as tk

class Marquee(tk.Canvas):
    def __init__(self, parent, text, margin=2, borderwidth=1, relief='flat', fps=30):
        tk.Canvas.__init__(self, parent, borderwidth=borderwidth, relief=relief)
        self.fps = fps

        # start by drawing the text off screen, then asking the canvas
        # how much space we need. Use that to compute the initial size
        # of the canvas. 
        text = self.create_text(0, -1000, text=text, anchor="w", tags=("text",))
        (x0, y0, x1, y1) = self.bbox("text")
        width = (x1 - x0) + (2*margin) + (2*borderwidth)
        height = (y1 - y0) + (2*margin) + (2*borderwidth)
        self.configure(width=width, height=height)

        # start the animation
        self.animate()

    def animate(self):
        (x0, y0, x1, y1) = self.bbox("text")
        if x1 < 0 or y0 < 0:
            # everything is off the screen; reset the X
            # to be just past the right margin
            x0 = self.winfo_width()
            y0 = int(self.winfo_height()/2)
            self.coords("text", x0, y0)
        else:
            self.move("text", -1, 0)

        # do again in a few milliseconds
        self.after_id = self.after(int(1000/self.fps), self.animate)

root = tk.Tk()
marquee = Marquee(root, text="Hello, world", borderwidth=1, relief="sunken")
marquee.pack(side="top", fill="x", pady=20)

root.mainloop()

答案 2 :(得分:0)

我没有Tkinter的经验,但是我希望这可以帮助您了解如何在代码中实现。谢谢!

import sys
import time


def main():
    program = animate()
    program.data = input("Enter string : ") + " "
    program.width = int(input("Enter width : "))
    program.animate()


class animate:
    def __init__(self):
        self.data = ""
        self.width = 0

    def animate(self):
        try:
            while True:
                for x in range(0, self.width):
                    time.sleep(0.1)
                    msg = "\r{}{}".format(" " * x, self.data)
                    sys.stdout.write(msg)
                    sys.stdout.flush()

                for x in range(self.width, 0, -1):
                    time.sleep(0.1)
                    msg = "\r{}{}".format(" " * x, self.data)
                    sys.stdout.write(msg)
                    sys.stdout.flush()
        except KeyboardInterrupt:
            print("Exiting")


if __name__ == "__main__":
    main()