Python GUI tkinter 学习笔记(三)
草稿
# -*- coding: utf-8 -*-
from Tkinter import *
root = Tk()
Label(root, text = "First").grid(row=0)
Label(root, text = "Second").grid(row=1)
e1 = Entry(root)
e2 = Entry(root)
e1.grid(row = 0,column = 1)
e2.grid(row = 1,column = 1)
mainloop()
差不多了可以照模板写了
# -*- coding: utf-8 -*-
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
# 主窗口里面再创建一个frame
self.fm_1 = Frame(frame)
self.fm_1.pack()
# 设置一个label
l1 = Label(self.fm_1, text="url:").grid(row=0)
# 设置一个输入框
e1 = Entry(self.fm_1, borderwidth=3)
e1.grid(row=0, column=1)
# 添加一个文本框,作为函数运行的输出
# 添加一个self可以在其他定义的函数进行调用
self.text = Text(self.fm_1)
self.text.grid(row=3, column=0, columnspan=3, sticky=N + S)
# 添加一个按钮
b1 = Button(self.fm_1,
text="命令执行",
command=self.cmd).grid(row=0,
column=2)
'''
# 添加一个输出框,将函数运行结果输出到界面
v1 = StringVar()
e2 = Entry(fm_1 ,textvariable = v1,borderwidth = 3, )
v1.set("normal")
e2.grid(row=2, column =0,columnspan = 3,rowspan = 5)
'''
frame.pack()
def cmd(self):
self.text.insert(INSERT, "ttt")
return "this is a test"
root = Tk()
# 设置窗体名称
root.title("test")
# 设置窗体大小
# root.geometry('300x300')
app = App(root)
root.mainloop()
root.destroy()