300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > python界面GUI设计 tkinter设计界面初步教程

python界面GUI设计 tkinter设计界面初步教程

时间:2023-03-22 02:33:21

相关推荐

python界面GUI设计 tkinter设计界面初步教程

如果没有时间仔细看完这篇文章的话可以现在下载PAGE+TCL然后直接输入最后代码运行程序即可。

python可以使用tkinter库来实现小程序制作,还是很方便的,比较大型程序可以采用pyqt来制作GUI。

我在一开始的时候不知道tkinter可以直接用一个可视化的界面来进行界面设计,当时是直接用代码来生成窗口、设计控件,再设计功能,感觉效率不是很高。因此我觉得要想做GUI提高效率还是需要用到可视化界面来拖放控件快速生成一个窗口来供我们设计代码,这样既直观,还比较爽。

要想实现这点的第一步方法是下载两个软件,一个是支持库Tcl,一个是GUI设计软件PAGE。下载完这两个软件之后就可以进行空间拖放来制作GUI。

PAGE

/

Tcl(8.6+)

/activetcl/downloads

下载安装完后打开软件如下图

之后随便设计一个窗口来测试。

点击Gen_python->Generate Python GUI来生成代码。注意到直接生成的代码是不能用的。需要修改掉一些没用的代码。

这里需要把与_support有关的代码全部删除,把下面这些代码删除,注意到里面有1_support这个是不能用的代码,1_support是我的文件名为1.py所以软件自动生成的代码为1_support。这个是不能用的代码。

所以要想使程序运行需要删除下列代码。

w = Nonedef create_Toplevel1(rt, *args, **kwargs):'''Starting point when module is imported by another module.Correct form of call: 'create_Toplevel1(root, *args, **kwargs)' .'''global w, w_win, root#rt = rootroot = rtw = tk.Toplevel (root)top = Toplevel1 (w)1_support.init(w, top, *args, **kwargs)return (w, top)def destroy_Toplevel1():global ww.destroy()w = None

最终代码为(经过测试可以直接运行的代码):

import systry:import Tkinter as tkexcept ImportError:import tkinter as tktry:import ttkpy3 = Falseexcept ImportError:import tkinter.ttk as ttkpy3 = Truedef vp_start_gui():'''Starting point when module is the main routine.'''global val, w, rootroot = tk.Tk()top = Toplevel1 (root)root.mainloop()class Toplevel1:def __init__(self, top=None):'''This class configures and populates the toplevel window.top is the toplevel containing window.'''_bgcolor = '#d9d9d9' # X11 color: 'gray85'_fgcolor = '#000000' # X11 color: 'black'_compcolor = '#d9d9d9' # X11 color: 'gray85'_ana1color = '#d9d9d9' # X11 color: 'gray85'_ana2color = '#ececec' # Closest X11 color: 'gray92'top.geometry("600x450+600+258")top.minsize(152, 1)top.maxsize(1924, 1055)top.resizable(1, 1)top.title("New Toplevel")top.configure(background="#d9d9d9")self.Button1 = tk.Button(top)self.Button1.place(relx=0.267, rely=0.2, height=53, width=153)self.Button1.configure(activebackground="#ececec")self.Button1.configure(activeforeground="#000000")self.Button1.configure(background="#d9d9d9")self.Button1.configure(disabledforeground="#a3a3a3")self.Button1.configure(foreground="#000000")self.Button1.configure(highlightbackground="#d9d9d9")self.Button1.configure(highlightcolor="black")self.Button1.configure(pady="0")self.Button1.configure(text='''设置python''')self.Label1 = tk.Label(top)self.Label1.place(relx=0.15, rely=0.489, height=86, width=265)self.Label1.configure(background="#d9d9d9")self.Label1.configure(disabledforeground="#a3a3a3")self.Label1.configure(foreground="#000000")self.Label1.configure(text='''Label''')if __name__ == '__main__':vp_start_gui()

程序运行结果如下图所示:

感觉效果还不错,但是要想设计一些style的话我个人认为还是Pyqt方便(因为我以前使用QT多一些,对QT相对而言熟练)。其中一点要考虑的是Tkinter的很多教程在网上不容易搜到,官方文档有点晦涩,需要较强的英文理解能力,这方面在国外的教程很多,国内比较少。

不过Tkinter做小的程序真的是很方便,代码比较简洁。

另外最重要的是,这是python的标准库,兼容性没得说,兼容性很好。任何函数直接套就可以用。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。