python – 使用Tkinter的GUI应用程序 – 拖放
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 使用Tkinter的GUI应用程序 – 拖放,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3850字,纯文字阅读大概需要6分钟。
内容图文
![python – 使用Tkinter的GUI应用程序 – 拖放](/upload/InfoBanner/zyjiaocheng/784/cbdfd9fca55c4a5aabdb421f3759652b.jpg)
我最近一直在使用WxPython来创建一个像Cisco数据包跟踪器这样的GUI网络模拟器,但如果我说实话,那么我很难找到我需要的例子等等.Iv又回到了老忠实的Tk.
到目前为止,我的程序有一个菜单栏,其中包含一个文件>出口.它还在应用程序的右下方有一个Exit按钮.除此之外,它还有一个设定尺寸的画布和各种按钮,点击它们时会在画布上产生一个小硬件图像.这是使用PIL完成的
我接下来需要的是能够在画布上拖动这些图像,这证明有点困难.我已经看了下面的例子,它是如何分解的,我有点理解你是如何需要点击定义,动作(从a到b)和发布定义,但是我如何将它应用到我的代码中我已经有了?
这是我上面引用的链接:
http://www.python-forum.org/pythonforum/viewtopic.php?f=4&p=75789
最后这是我已经有的代码.我可以理解我的代码的布局和结构并不是很好,因为我对编程很新,但任何指导/示例/视觉表示都会很棒.
from Tkinter import*
from PIL import Image, ImageTk
class AllTkinterWidgets:
def __init__(self, master):
frame= Frame(master, width=900, height=600)
frame.pack()
iframe5 = Frame (frame, bd=2, relief=RAISED)
iframe5.pack(expand=1, fill=X, pady=10, padx=5)
c = Canvas(iframe5, bg='white', width=600, height=500)
c.pack()
# definitions to print hardware images to the canvas
# -----------------------------------------------------------------------
def show_imageRouter():
c.create_image(30,30, image=image1)
def show_imageSwitch():
c.create_image(30,60, image=image2)
def show_imageServer():
c.create_image(30,100, image=image3)
def show_imageIpPhone():
c.create_image(30,140, image=image4)
def show_imageWirelessRouter():
c.create_image(30,180, image=image5)
def show_imageHost():
c.create_image(30, 220, image=image6)
# Network hardware buttons created
# ----------------------------------------------------
self.button = Button(frame, text = "Router", height= 1, width= 8, padx=2, pady=2,command=show_imageRouter)
self.button.pack(side = LEFT)
self.button = Button(frame, text = "Switch",height= 1, width= 8, padx=2, pady=2, command=show_imageSwitch)
self.button.pack(side = LEFT)
self.button = Button(frame, text = "Server",height= 1, width= 8, padx=2, pady=2, command=show_imageServer)
self.button.pack(side = LEFT)
self.button = Button(frame, text = "IP Phone",height= 1, width= 8, padx=2, pady=2, command=show_imageIpPhone)
self.button.pack(side = LEFT)
self.button = Button(frame, text = "Wireless Router",height= 1, width= 12, padx=2, pady=2, command=show_imageWirelessRouter)
self.button.pack(side = LEFT)
self.button = Button(frame, text = "Host",height= 1, width= 8, padx=2, pady=2, command=show_imageHost)
self.button.pack(side = LEFT)
self.button = Button(frame, text = "Cabling",height= 1, width= 8, padx=2, pady=2)
self.button.pack(side = LEFT)
self.button = Button(frame, text = "Square",height= 1, width= 8, padx=2, pady=2)
self.button.pack(side = LEFT)
# Create the image objects for the hardware Images
# ----------------------------------------------------------------------
imageFile = "router.png"
image1 = ImageTk.PhotoImage(Image.open(imageFile))
imageFile = "switch.png"
image2 = ImageTk.PhotoImage(Image.open(imageFile))
imageFile = "Server.png"
image3 = ImageTk.PhotoImage(Image.open(imageFile))
imageFile = "ipPhone.png"
image4 = ImageTk.PhotoImage(Image.open(imageFile))
imageFile = "WirelessRouter.png"
image5 = ImageTk.PhotoImage(Image.open(imageFile))
imageFile = "Host.png"
image6 = ImageTk.PhotoImage(Image.open(imageFile))
root = Tk()
all = AllTkinterWidgets(root)
def Exit():
print "Exit"
# Create an Exit Button
toolbar = Frame(root)
b = Button(toolbar, text="Exit", width=6, height=3, command=Exit)
b.pack(side=RIGHT, padx=2, pady=2)
toolbar.pack(side=BOTTOM, fill=X)
# Press Esc to quit
root.bind("<Escape>", exit)
# Creation of a menu File > Exit
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Exit", command=Exit)
root.mainloop()
对不起,如果缩进有点奇怪.我调整它以使它在这里阻塞.
解决方法:
This answer到问题“用于移动椭圆的绘图代码”显示了如何在画布上拖动对象.
内容总结
以上是互联网集市为您收集整理的python – 使用Tkinter的GUI应用程序 – 拖放全部内容,希望文章能够帮你解决python – 使用Tkinter的GUI应用程序 – 拖放所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。