python
  • python-3.x
  • tkinter
  • 2013-07-27 43 views 0 likes 
    0

    这是我的工作代码:Python:“选择”功能似乎没有按照我的要求工作。

    import sys 
    from tkinter import * 
    from random import choice 
    def motiv(): 
        motiv1 = mLabel = Label(text='Waste not, want not', fg="red").pack() 
        motiv2 = mLabel = Label(text='Sticks and stones', fg="red").pack() 
        motiv3 = mLabel = Label(text='Keep holding on', fg="red").pack() 
        motiv4 = mLabel = Label(text='Hold On, Pain Ends', fg="red").pack() 
    
        ranMotiv = [motiv1, motiv2, motiv3, motiv4] 
        print (choice(ranMotiv)) 
    
    mGui = Tk() 
    
    mGui.geometry('450x450+500+150') 
    mGui.title('RMM') 
    
    mLabel = Label(text='Welcome to the Random Motivation Machine', fg="red").pack() 
    
    mButton = Button(text = "Click for Some Motivation", command = motiv) 
    mButton.pack() 
    
    mGui.mainloop() 
    

    有没有错误,但它使打印出所有这些案文在同一时间,当我是想只打印出来只有一个他们随机。

    我的目标是让某人按下按钮,然后在GUI窗口中弹出一个随机短语。

    所以有人按下按钮,只是任何的四个文本短语的一个出来的窗口:

    1.Waste,吃穿不缺。

    2.坚持和结石

    3.坚持下去。

    4.持续,疼痛结束。

    我相信我的烦恼是从这个区域就在这里出现:

    ranMotiv = [motiv1, motiv2, motiv3, motiv4] 
    print (choice(ranMotiv)) 
    

    有没有人有什么想法?这只是我的一个非常小的宠物项目。我只用了不到几个月的Python,所以我不太聪明。顺便说一句,我正在运行Python 3.2.5。谢谢大家。

    +1

    请勿立即打包标签。创建一个文本列表,然后在其中一个'random.choice'中,将其转换为标签并打包 – inspectorG4dget

    +0

    非常感谢。我希望我早一点想到这一点。这绝对有很多意义。我只是实现了你的建议,它的运行非常好,只是我想要的。再次谢谢你。 – CaptainProdigy

    回答

    0

    如何:

    from tkinter import * 
    from random import choice 
    
    # Place the messages outside of the function so that they are not 
    # re-created each time the button is pressed 
    messages = ['Waste not, want not', 'Sticks and stones', 
    'Keep holding on', 'Hold On, Pain Ends'] 
    
    def motiv(): 
    
        # Just update the text of the Label instead of create a whole new one 
        message["text"] = choice(messages) 
    
    mGui = Tk() 
    
    mGui.geometry('450x450+500+150') 
    mGui.title('RMM') 
    
    mLabel = Label(text='Welcome to the Random Motivation Machine', fg="red").pack() 
    
    mButton = Button(text = "Click for Some Motivation", command = motiv) 
    mButton.pack() 
    
    # Make a Label to hold the messages that will be updated with each click of the button 
    message = Label(fg="red") 
    message.pack() 
    
    mGui.mainloop() 
    

    而不是仅仅套结新消息发送到GUI的底部,这种方法更简洁,只是更新消息的文本。它还修复了消息从GUI运行的问题(您可以通过单击按钮30次来查看)。

    +0

    我跑的代码,它的工作原理是我想要的,但我有点困惑,在motiv函数中消息[“text”] = choice(messages)是什么意思。具体是什么信息[“文本”]在做什么/它是什么?对不起,正如我所说,我在这里没有经验。谢谢。 – CaptainProdigy

    +0

    @ZacHall:当然可以。 'message'是Label实例。 'message [“text”]'是message的文本选项。你也可以做'message [“fg”] =“green”'将前景色改为绿色。基本上,任何Tkinter标签(或任何其他Tkinter小部件)具有的选项都可以像这样配置。要查看所有可用的选项,可以调用Tkinter小部件的'keys'方法,如下所示:' .keys()'。 – iCodez

    1

    我最初发布这个作为一个评论,但它竟然是答案,所以我在这里重新张贴它:

    的问题是,Label(text='Waste not, want not', fg="red").pack()包装标签的时候了。这样做与所有标签导致他们被打包。不要紧,如果以后再打random.choice是因为标签已经打包到您的GUI中。

    如果你想从标签池中创建一个随机标签,你想要做的是这样的:

    def motiv(): 
        myLabels = ['Waste not, want not', 'Sticks and stones', 'Keep holding on', 'Hold On, Pain Ends'] 
        chosenLabel = random.choice(myLabels) 
        randMotiv = Label(text=chosenLabel, fg="red").pack() 
    
    +0

    您是否打算在创建标签小部件时使用'chosenLabel'? –

    +0

    @BryanOakley:很好!感谢bugreport。固定! – inspectorG4dget

    相关问题