简体   繁体   English

使用 Tkinter 创建按钮

[英]Creating a button using Tkinter

I have attached part of the code I'm modifying.我附上了我正在修改的部分代码。 I would like to create a button using Tkinter such that , based on the option I sect using the button (A or B), I would like to resize the image in the line cv_image = image[:,:,:] .我想使用 Tkinter 创建一个按钮,以便根据我使用按钮(A 或 B)选择的选项,我想调整cv_image = image[:,:,:]行中的图像大小。

What is the best way to do this.做这个的最好方式是什么。 Thanks in advance.提前致谢。

class image_converter:

  def __init__(self):
    print 'show window'
    cv2.namedWindow("Image window", 1)
    print 'start bridge and subscribe'
    self.bridge = CvBridge()
    print Image
    self.image_sub = rospy.Subscriber("/MDS_CamServer/camera/image",Image,self.callback)
    self.save = False;
    self.count=0;
    self.X=np.array([[]]);
    self.y=np.array([[]]);
    self.X_new=np.array([[]]);
    self.y_new=np.array([[]]);

  def callback(self,data):
    try:
      image = self.bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError, e:
      print e

    cv_image = image[:,:,:];


def main(args):
  ic = image_converter()
  rospy.init_node('image_converter', anonymous=True)
  try:
    rospy.spin()
  except KeyboardInterrupt:
    print "Shutting down"
  cv2.destroyAllWindows()

if __name__ == '__main__':
    main(sys.argv)

您应该创建一个单选按钮来选择用户想要选择 A 或 B 的选项,然后选择将用户重定向到特定宽度

You can try this code snippet你可以试试这个代码片段

self.v1 = IntVar()
Label(master, text="""resize the image in the line with """,justify = LEFT, padx = 20).pack()
Radiobutton(master, text="A",padx = 20, variable=self.v1, value=1).pack(anchor=W)
Radiobutton(master, text="B", padx = 20, variable=self.v1, value=2).pack(anchor=W)
if self.v2.get() == 1:
   Imgwidth = 100 #specify it as per A
else:
   Imgwidth = 200 #specify it as per B
Imgheight = 100#just specify or use original 
image = Image.open(Image_Location)
image = image.resize((Imgheight, Imgwidth), Image.ANTIALIAS) #The (250, 250) is (height, width)
self.pw.pic = ImageTk.PhotoImage(image)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM