【发布时间】:2016-02-05 15:39:58
【问题描述】:
您好,我有一个使用 Tkinter 编写的 GUI,代码模板如下。我的问题是 PyCharm 警告我的函数(def func1、def func2)它们是静态的。为了摆脱警告,我将 @staticmethod 放在了函数上方。这是做什么的,有必要吗?
# Use TKinter for python 2, tkinter for python 3
import Tkinter as Tk
import ctypes
import numpy as np
import os, fnmatch
import tkFont
class MainWindow(Tk.Frame):
def __init__(self, parent):
Tk.Frame.__init__(self,parent)
self.parent = parent
self.parent.title('BandCad')
self.initialize()
@staticmethod
def si_units(self, string):
if string.endswith('M'):
num = float(string.replace('M', 'e6'))
elif string.endswith('K'):
num = float(string.replace('K', 'e3'))
elif string.endswith('k'):
num = float(string.replace('k', 'e3'))
else:
num = float(string)
return num
if __name__ == "__main__":
# main()
root = Tk.Tk()
app = MainWindow(root)
app.mainloop()
【问题讨论】:
-
如果您的方法实际上并未引用
self,PyCharm 会向您发出警告。@staticmethod只是表示没有通过实例的方法,通常命名为self。从您发布的代码中,很难添加任何其他内容。 -
@jonrsharpe。谢谢你的评论。我编辑了我的代码。这是否有助于为您提供更多信息来回答。
-
不需要更多信息;就是这样。
标签: python-2.7 tkinter pycharm