需求: python都是黑屏的cmd命令, 不利于使用, 尝试直接用python原始界面接口写一些简单界面尝试
实现考虑: 不使用大型框架,需要一个最小,最精简的界面库来做工具开发, pyqt, wxwidght等都由几十兆, 太大, 选择使用tkinter 不足10M, python原生自带, 不需要高大上控件,只需要基本各种输入控件控制流程即可.
实现: 1: 首先使用 手动拖动控件方式 设计好布局界面 这一步使用 Visual TK, Visual Python Tkinter GUI Creator 这个网站
2: 实现基本控件响应函数
完整实现代码
# Name: contain_image.py
# Description: small image is in big image
# Author: CHIJING 20230510
# -*- coding: utf-8 -*-
#
import sys as sys
import tkinter as tk
import tkinter.font as tkFont
from tkinter import filedialog
from tkinter import *
class App:
def __init__(self, root):
#setting title
root.title("模型纹理处理工具")
self.patha = StringVar(root,"D:/temp/obj","a")
self.pathb = StringVar(root,"D:/temp/roof","b")
self.pathc = StringVar(root,"D:/temp/wall","c")
#setting window size
self.width=612
self.height=173
self.screenwidth = root.winfo_screenwidth()
self.screenheight = root.winfo_screenheight()
self.alignstr = '%dx%d+%d+%d' % (self.width, self.height, (self.screenwidth - self.width) / 2, (self.screenheight - self.height) / 2)
root.geometry(self.alignstr)
root.resizable(width=False, height=False)
self.GLabel_349=tk.Label(root)
ft = tkFont.Font(family='Times',size=10)
self.GLabel_349["font"] = ft
self.GLabel_349["fg"] = "#333333"
self.GLabel_349["justify"] = "center"
self.GLabel_349["text"] = "obj目录"
self.GLabel_349.place(x=10,y=10,width=70,height=25)
self.GLabel_746=tk.Label(root)
#ft = tkFont.Font(family='Times',size=10)
self.GLabel_746["font"] = ft
self.GLabel_746["fg"] = "#333333"
self.GLabel_746["justify"] = "center"
self.GLabel_746["text"] = "窗子纹理"
self.GLabel_746.place(x=10,y=50,width=70,height=25)
self.GLabel_466=tk.Label(root)
ft = tkFont.Font(family='Times',size=10)
self.GLabel_466["font"] = ft
self.GLabel_466["fg"] = "#333333"
self.GLabel_466["justify"] = "center"
self.GLabel_466["text"] = "墙面纹理"
self.GLabel_466.place(x=10,y=90,width=70,height=25)
self.GButton_267=tk.Button(root)
self.GButton_267["bg"] = "#f0f0f0"
ft = tkFont.Font(family='Times',size=10)
self.GButton_267["font"] = ft
self.GButton_267["fg"] = "#000000"
self.GButton_267["justify"] = "center"
self.GButton_267["text"] = "确定"
self.GButton_267.place(x=450,y=130,width=70,height=25)
self.GButton_267["command"] = self.GButton_267_command
self.GButton_586=tk.Button(root)
self.GButton_586["bg"] = "#f0f0f0"
ft = tkFont.Font(family='Times',size=10)
self.GButton_586["font"] = ft
self.GButton_586["fg"] = "#000000"
self.GButton_586["justify"] = "center"
self.GButton_586["text"] = "取消"
self.GButton_586.place(x=530,y=130,width=70,height=25)
self.GButton_586["command"] = self.GButton_586_command
self.GButton_419=tk.Button(root)
self.GButton_419["bg"] = "#f0f0f0"
ft = tkFont.Font(family='Times',size=10)
self.GButton_419["font"] = ft
self.GButton_419["fg"] = "#000000"
self.GButton_419["justify"] = "center"
self.GButton_419["text"] = "选择"
self.GButton_419.place(x=530,y=10,width=67,height=30)
self.GButton_419["command"] = self.GButton_419_command
self.GLineEdit_460=tk.Entry(root,textvariable = self.patha)
self.GLineEdit_460["borderwidth"] = "0px"
ft = tkFont.Font(family='Times',size=10)
#self.GLineEdit_460["font"] = ft
#self.GLineEdit_460["fg"] = "#333333"
#self.GLineEdit_460["justify"] = "left"
#self.GLineEdit_460["text"] = "Entry"
self.GLineEdit_460.place(x=90,y=10,width=431,height=30)
self.GLineEdit_525=tk.Entry(root,textvariable=self.pathb)
self.GLineEdit_525["borderwidth"] = "0px"
ft = tkFont.Font(family='Times',size=10)
#self.GLineEdit_525["font"] = ft
#self.GLineEdit_525["fg"] = "#333333"
#self.GLineEdit_525["justify"] = "left"
#self.GLineEdit_525["text"] = "Entry"
self.GLineEdit_525.place(x=90,y=50,width=431,height=30)
self.GLineEdit_410=tk.Entry(root,textvariable=self.pathc)
self.GLineEdit_410["borderwidth"] = "0px"
ft = tkFont.Font(family='Times',size=10)
#self.GLineEdit_410["font"] = ft
#self.GLineEdit_410["fg"] = "#333333"
#self.GLineEdit_410["justify"] = "left"
#self.GLineEdit_410["text"] = "Entry"
self.GLineEdit_410.place(x=90,y=90,width=432,height=30)
self.GButton_526=tk.Button(root)
self.GButton_526["bg"] = "#f0f0f0"
ft = tkFont.Font(family='Times',size=10)
self.GButton_526["font"] = ft
self.GButton_526["fg"] = "#000000"
self.GButton_526["justify"] = "center"
self.GButton_526["text"] = "选择"
self.GButton_526.place(x=530,y=50,width=70,height=25)
self.GButton_526["command"] = self.GButton_526_command
self.GButton_3=tk.Button(root)
self.GButton_3["bg"] = "#f0f0f0"
ft = tkFont.Font(family='Times',size=10)
self.GButton_3["font"] = ft
self.GButton_3["fg"] = "#000000"
self.GButton_3["justify"] = "center"
self.GButton_3["text"] = "选择"
self.GButton_3.place(x=530,y=90,width=70,height=25)
self.GButton_3["command"] = self.GButton_3_command
def GButton_267_command(self):
print(self.GLineEdit_460.get())
print(self.GLineEdit_525.get())
print(self.GLineEdit_410.get())
def GButton_586_command(self):
try:
sys.exit(0)
except:
print('Program is dead.')
finally:
print('clean-up')
def GButton_419_command(self):
selected_folder = tk.filedialog.askdirectory() # 使用askdirectory函数选择文件夹
self.patha.set(selected_folder)
def GButton_526_command(self):
selected_folder = tk.filedialog.askdirectory() # 使用askdirectory函数选择文件夹
self.pathb.set(selected_folder)
def GButton_3_command(self):
selected_folder = tk.filedialog.askdirectory() # 使用askdirectory函数选择文件夹
self.pathc.set(selected_folder)
pass
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()