我试图为twitter创建一个python gui,它将我的文本框作为推文提交

时间:2016-06-26 17:24:59

标签: python twitter tkinter

以下是我使用的代码:

from Tkinter import *
import tweepy
from secrets import *

auth = tweepy.OAuthHandler(C_KEY, C_SECRET)
auth.set_access_token(A_TOKEN, A_TOKEN_SECRET)
api = tweepy.API(auth)

class MyFirstGUI(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.instructions = Label(self, text="Enter your tweet")
        self.instructions.grid(row = 0, column = 0, columnspan = 2, sticky = W)

        self.tweet = Entry(self)
        self.tweet.grid(row=1, column = 0, sticky = W)

        self.submit_button = Button(self, text="Tweet", command = self.post)
        self.submit_button.grid(row = 2, column = 0, sticky=W)

    def post(self):
        content = self.post

        api.update_status(content)

root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()

每当我尝试推文时,它都会发布<bound method http://MyFirstGUI.post of <__main__.MyFirstGUI instance at 0x0000000003BFF688>>之类的内容,而不是我在文本框中添加的内容。知道到底发生了什么事吗?

1 个答案:

答案 0 :(得分:0)

Entry应该是您def post(self): api.update_status(self.tweet.get()) 的内容:

import Foundation

final class DataModel: NSObject {

  static let shared = DataModel()

  var isSleepTimerOn = false
  var timerTime: NSTimeInterval = 100.0

}

// The second view controller.

import UIKit

class TimerViewController: UIViewController {

  @IBOutlet weak var timerLabel: UILabel!
  @IBOutlet weak var timerSwitch: UISwitch!

  var timer: NSTimer?

  override func viewDidLoad() {
    super.viewDidLoad()

    timerSwitch.on = DataModel.shared.isSleepTimerOn
    timerLabel.text = String(DataModel.shared.timerTime)

    let selector = #selector(setTimerLabel), name = "setTimerLabel"
    NSNotificationCenter.defaultCenter().addObserver(self, selector: selector, name: name, object: nil)
  }

  @IBAction func switchToggled(sender: AnyObject) {
    DataModel.shared.isSleepTimerOn = timerSwitch.on

    switch timerSwitch.on {
    case true:
      startTimer()
    case false:
      stopTimer()
    }
  }

  func startTimer() {
    let selector = #selector(decrementTimer)
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: selector, userInfo: nil, repeats: true)
    NSRunLoop.mainRunLoop().addTimer(timer!, forMode: NSRunLoopCommonModes)
  }

  func decrementTimer() {
    DataModel.shared.timerTime -= 1
    NSNotificationCenter.defaultCenter().postNotificationName("setTimerLabel", object: nil)

    setTimerLabel()
  }

  func setTimerLabel() {
    timerLabel.text = String(DataModel.shared.timerTime)
  }

  func stopTimer() {
    timer?.invalidate()
    timer = nil

    DataModel.shared.timerTime = 100.0
    timerLabel.text = String(DataModel.shared.timerTime)

    NSNotificationCenter.defaultCenter().removeObserver(self, name: "setTimerLabel", object: nil)
  }

}