28

简书文章下载器-2

 5 years ago
source link: https://www.tuicool.com/articles/RNjErmf
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

欢迎关注我的专栏( つ•̀ω•́)つ 【人工智能通识】

主要工具

  • 编写工具:VSCode
  • 编写语言:Python
  • 项目管理:Github+Git
  • 编写界面:Tkinter

初始化项目

git init
git config --global user.name "zhyuzh"
git config --global user.email "[email protected]"
git remote add origin https://username:[email protected]/zhyuzh3d/JianshuDownloader.git
git pull origin master
#JianshuDownloader
初始化

测试界面

对于Python3.x,Tkinter已经是标准内置模块,可以直接使用而不需要单独安装。

修改main.py文件,添加以下内容进行测试:

from tkinter import *
from tkinter import ttk
import random

root = Tk()
root.title('MyApp')
root.resizable(width=False, height=False)
root.config(background='#EEE')
root.geometry('300x150')

def gen():
    val.set(repr(random.random()))

val = StringVar()
val.set('3.14')
ttk.Frame(root, height=20).grid()
lb=ttk.Entry(root,textvariable=val).grid(row=1, column=1, pady=10, padx=10,ipady=5,sticky='nsew')
bt = ttk.Button(root, text='Random', width=20, command=gen).grid(
    row=2, column=1, ipady=10, ipadx=10, sticky=E)

root.mainloop()

可以用 python main.py 来运行这段代码,也可以使用Run Code插件来运行,这会弹出一个小窗口,上面有个按钮,点击将随机改变输入框内的数字。

EbAVnu7.png!web

几点说明:

  • root = Tk() 是初始化界面窗口,最后要 mainloop() 启动它
  • root.geometry('300x150') 这个奇葩写法可以设定窗口的尺寸
  • def gen(): 定义了gen函数,并在下面 command=gen 当按钮点击时候调用它
  • val = StringVar() 初始化一个字符串变量,并在 textvariable=val 动态绑定它
  • grid(row=2, column=1, ipady=10, ipadx=10, sticky=E) 这是添加到画面并设定位置,按照row-col行列模式,ipady和ipadx是内边距, sticky=E 这里的E是指东面右侧对齐

后续我们再深入使用Tkinter界面工具。

欢迎关注我的专栏( つ•̀ω•́)つ 【人工智能通识】

每个人的智能新时代

如果您发现文章错误,请不吝留言指正;

如果您觉得有用,请点喜欢;

如果您觉得很有用,欢迎转载~

END


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK