获取追溯和AttributeError:'NoneType'对象没有属性'get'

时间:2014-02-20 16:13:21

标签: python user-interface attributes tkinter traceback

所以我试图使用Tkinter和python制作一个简单的计算器程序。我有一些通用代码,用于加法和减法,但我得到了这个错误。请注意,代码如下。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python32\lib\tkinter\__init__.py", line 1399, in __call__
    return self.func(*args)
  File "C:\Users\**\workspace\calcApp\calcApp\guiCalc.py", line 21, in numClick
    input = int(entry.get())
AttributeError: 'NoneType' object has no attribute 'get'

guiCalc.py

from tkinter import *

class Calc:
 def init():

  root = Tk()
  root.wm_title("Calculator")

  input = 0
  varIn = StringVar()
  varIn = ""
  labelText = StringVar()
  ans = ""
  ans2 = ""

  entry = Entry(root).grid()

  def numClick():
   input = int(entry.get()) 
   entry.delete(0, END)   


  def equalClick():
   if(entry.get()=="+"):
    ans = input + int(entry.get())
    label.configure(text=ans)
   if(entry.get()=="-"):
    ans2 = input-int(entry.get())
    label.configure(text = ans2)

  Button(root, text="+", command=numClick).grid()    
  Button(root, text="-", command=numClick).grid()
  Button(root, text="=", command =equalClick).grid()

  label = Label(root, text="")
  label.grid()
  root.mainloop()



Calc.init()

2 个答案:

答案 0 :(得分:3)

entry = Entry(root).grid()

entry此处为None,因为grid不会返回任何内容。也许你打算这样做:

entry = Entry(root)
entry.grid()

答案 1 :(得分:0)

我有点新鲜,是的,但是让我分享一下我学到的东西。

在小部件的创建行中附加private String jsonResult; public String url = "http://111.111.111.1/androidapp/getjson.php"; ListView listView; List<Map<String, String>> agents = new ArrayList<Map<String, String>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); accessWebService(); } private class JsonReadTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(params[0]); try { HttpResponse response = httpclient.execute(httppost); jsonResult = inputStreamToString( response.getEntity().getContent()).toString(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } private StringBuilder inputStreamToString(InputStream is) { String rLine = ""; StringBuilder answer = new StringBuilder(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); try { while ((rLine = rd.readLine()) != null) { answer.append(rLine); } } catch (IOException e) { Toast.makeText(getApplicationContext(), "error ..." + e.toString(), Toast.LENGTH_LONG).show(); } return answer; } @Override protected void onPostExecute(String result) { ListDrwaer(); } } public void accessWebService() { JsonReadTask task = new JsonReadTask(); task.execute(new String[]{url}); } public void ListDrwaer() { try { JSONObject jsonResponse = new JSONObject(jsonResult.substring(jsonResult.indexOf("{"), jsonResult.lastIndexOf("}") + 1)); JSONArray jsonMainNode = jsonResponse.optJSONArray("agents"); final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map; for (int i = 0; i < jsonMainNode.length(); i++) { JSONObject c = jsonMainNode.getJSONObject(i); map = new HashMap<String, String>(); map.put("AgtFirstName", c.getString("AgtFirstName")); map.put("AgtLastName", c.getString("AgtLastName")); MyArrList.add(map); SimpleAdapter simpleAdapter; simpleAdapter = new SimpleAdapter(MainActivity.this, MyArrList, R.layout.activity_column, new String[]{"AgtFirstName", "AgtLastName"}, new int[]{R.id.AgtFirstName , R.id.AgtLastName}); listView.setAdapter(simpleAdapter); } } catch (JSONException e) { Toast.makeText(getApplicationContext(), "error parsing..." + e.toString(), Toast.LENGTH_LONG).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent,View view, int position, long id) { Intent i=new Intent(MainActivity.this,AgentDetails.class); i.putExtra("position", map.get(position)); startActivity(i); } }; 大部分时间都可以完美地用于布局目的,但这不是一个好习惯。正确的做法是像凯文所说的那样建立新的一条线。