用tkinter做界面时,窗口出不来。
做了一个用pyautocad画cad图的程序,然后想做图形界面化,正在测试基本功能,现在在做的事是 先设定中心点坐标,然后打开有让中心点上移和在中心点画图的两个按钮的界面。但是现在运行程序,设定完中心点的xy坐标后,idle里一直在运行,界面不出来,就像是陷入死循环一样。是不是调图形界面就不能用input什么的?应该如何解决呢?
import tkinter as tk
import plant
class APP:
def __init__(self,master):
frame = tk.Frame(master)
frame.pack()
self.changeUpCP = tk.Button(frame,text='向上移动位置',fg ='blue',command = plant.aa.changeUpCP())
self.juxing = tk.Button(frame,text='画矩形',fg ='blue',command = plant.aa.juxing.plant())
self.changeUpCP.pack()
root =tk.Tk()
root.title('画流程图')
app = APP(root)
root.mainloop()
这里面导入的plant。py是这样的
from pyautocad import Autocad, APoint,aDouble
acad = Autocad(create_if_not_exists = True)
import math
import numpy as np
pyacad = Autocad(create_if_not_exists=True)
pyacad.prompt("Hello! AutoCAD from pyautocad.")
print(pyacad.doc.Name)
CPX = 500 #中心点 CenterPointX
CPY = 500 # CenterPointY
DeltaX = 50 #间距
DeltaY = 30
HW = 13 #HalfRWidth 矩形一半宽
HH = 6.5 #HalfRHight 矩形一半高
class SetPoint:
CPX = 500 #中心点 CenterPointX
CPY = 500 # CenterPointY
def creat(self):
self.juxing = Rectangle()
self.lingxing = Choose()
self.wenzi = InsertText()
def set_CP(self):
self.CPX = float(input('请输入x坐标:'))
self.CPY = float(input('请输入y坐标:'))
self.juxing.set_CP()
self.lingxing.set_CP()
self.wenzi.set_CP()
def set_Delta(self):
Delta = float(input('请设置间隔:'))
DeltaX = Delta + 2*HW
DeltaY = Delta + 2*HH
def set_RWH(self):
self.juxing.set_RWH()
def set_RCH(self):
self.lingxing.set_RWH()
def changeUpCP(self):
self.juxing.changeUpCP()
self.lingxing.changeUpCP()
self.wenzi.changeUpCP()
def changeDownCP(self):
self.juxing.changeDownCP()
self.lingxing.changeDownCP()
self.wenzi.changeDownCP()
def changeLeftCP(self):
self.juxing.changeLeftCP()
self.lingxing.changeLeftCP()
self.wenzi.changeLeftCP()
def changeRightCP(self):
self.juxing.changeRightCP()
self.lingxing.changeRightCP()
self.wenzi.changeRightCP()
class Rectangle:
HRW = 13 #HalfRWidth 矩形一半宽
HRH = 6.5 #HalfRHight 矩形一半高
DeltaX = 50 #间距
DeltaY = 30
def set_CP(self):
self.CPX = aa.CPX
self.CPY = aa.CPY
def set_Delta(self):
self.Delta = aa.Delta
self.DeltaX = Delta + 2*HW
self.DeltaY = Delta + 2*HH
def set_RWH(self):
self.HRW = float(input('请输入矩形的宽度:'))/2
self.HRH = float(input('请输入矩形的高度:'))/2
def changeUpCP(self):
self.CPY = self.CPY + self.DeltaY
def changeDownCP(self):
self.CPY = self.CPY - self.DeltaY
def changeLeftCP(self):
self.CPX = self.CPX - self.DeltaX
def changeRightCP(self):
self.CPX = self.CPX + self.DeltaX
def plant(self):
x =
y =
pnts = , y), APoint(x, y), APoint(x, y),
APoint(x, y), APoint(x, y), APoint(x, y),
APoint(x, y), APoint(x, y)]
pnts = # 将各点坐标顺序变换为行数据
pnts = aDouble(pnts)# 转化为双精度浮点数
plineObj = pyacad.model.AddPolyLine(pnts)
plineObj.Closed = True# 闭合多段线
class Choose:
HCW = 13 #HalfRWidth 矩形一半宽
HCH = 6.5 #HalfRHight 矩形一半高
DeltaX = 50 #间距
DeltaY = 30
def set_CP(self):
self.CPX = aa.CPX
self.CPY = aa.CPY
def set_Delta(self):
self.Delta = aa.Delta
self.DeltaX = Delta + 2*HW
self.DeltaY = Delta + 2*HH
def set_CWH(self):
self.HCW = float(input('请输入菱形的宽度:'))/2
self.HCH = float(input('请输入菱形的高度:'))/2
def changeUpCP(self):
self.CPY = self.CPY + self.DeltaY
def changeDownCP(self):
self.CPY = self.CPY - self.DeltaY
def changeLeftCP(self):
self.CPX = self.CPX - self.DeltaX
def changeRightCP(self):
self.CPX = self.CPX + self.DeltaX
def plant(self):
x =
y =
pnts = , y), APoint(x, y), APoint(x, y),
APoint(x, y), APoint(x, y), APoint(x, y),
APoint(x, y), APoint(x, y)]
pnts = # 将各点坐标顺序变换为行数据
pnts = aDouble(pnts)# 转化为双精度浮点数
plineObj = pyacad.model.AddPolyLine(pnts)
plineObj.Closed = True# 闭合多段线
class InsertText:
DeltaX = 50 #间距
DeltaY = 30
def set_CP(self):
self.CPX = aa.CPX
self.CPY = aa.CPY
def set_Delta(self):
self.Delta = aa.Delta
self.DeltaX = Delta + 2*HCW
self.DeltaY = Delta + 2*HCH
def changeUpCP(self):
self.CPY = self.CPY + self.DeltaY
def changeDownCP(self):
self.CPY = self.CPY - self.DeltaY
def changeLeftCP(self):
self.CPX = self.CPX - self.DeltaX
def changeRightCP(self):
self.CPX = self.CPX + self.DeltaX
def in_text(self):
p = APoint(self.CPX,self.CPY)
text = input('请输入要插入的文字')
#text = acad.model.AddText('Hi !' , p, 2.5)
insert = acad.model.AddText("{0}".format(text), p, 3)
aa=SetPoint()
aa.creat()
aa.set_CP()
本帖最后由 疾风怪盗 于 2020-9-23 16:52 编辑
你这个调用有问题啊,不是input的问题
plant里class类后面应该有()才对吧
一般这里不要实例化,一般写成:
if __name__ == '__main__':
aa = SetPoint()
aa.creat()
aa.set_CP()
调用时再引用对应方法
没有你的模块,所以只能注释掉模块,在类里面添加一个
def a(self):
print('1')
然后这个界面
command=plant.SetPoint().a
这样就能调用了 疾风怪盗 发表于 2020-9-23 16:50
你这个调用有问题啊,不是input的问题
plant里class类后面应该有()才对吧
一般这里不要实例化,一般写成 ...
后面尝试了一下我的代码窗口是能出来的,
请问不用实例化而用if __name__ == '__main__':的原因是什么呢?
另外 ,是command=plant.SetPoint().a么?为什么不是command=plant.SetPoint.a()呢?
我尝试了一下,如果用command = plant.aa.juxing.plant()没有执行该有的结果,但如果使用您说的command = plant.aa.juxing().plant,会报错啊?Traceback (most recent call last):
File "C:\Users\abc\Desktop\tuxingjiemian.py", line 24, in <module>
app = APP(root)
File "C:\Users\abc\Desktop\tuxingjiemian.py", line 12, in __init__
self.juxing = tk.Button(frame,text='画矩形',fg ='blue',command = plant.aa.juxing().plant)
TypeError: 'Rectangle' object is not callable 求资专用 发表于 2020-9-24 11:23
后面尝试了一下我的代码窗口是能出来的,
请问不用实例化而用if __name__ == '__main__':的原因是什么 ...
你要问我原理,讲不清楚,但就是这么用的
你要调用的是plant里类下面的某个方法,那就用
command=plant.SetPoint().a的形式,
不般不会去引用实例化后的对象,具体对不对,等其他大佬解释和指出错误
一般定义类,都是class SetPoint(),会带个括号
command等号后面跟着的是方法名,所以方法a会面没有括号
你的代码,我没有pyautocad 所以没法运行
但如果使用您说的command = plant.aa.juxing().plant,会报错啊?
我没有说用这样的形式,我说的是command=模块名.类名.方法名
页:
[1]