python
  • python-3.x
  • tkinter
  • 2013-07-27 45 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

    相關問題