day18:使用tkinter进行图形化编程入门

一、建立一个窗口

import tkinter

#创建一个主窗口
win = tkinter.Tk()

#设置标题
win.title("JJsPython")

#设置窗口大小,x not *
win.geometry("500x500+250+150")

#input get
e1 = tkinter.Variable()
#input win,use show to notshow
entry = tkinter.Entry(win,textvariable = e1)
entry.pack()
#init e1
e1.set("请输入密码")`在这里插入代码片`

#启动主窗口
win.mainloop()

注意在geometry设置中,是500x500,不是“*”号

如果是要输入密码的话,要设置隐藏:

#input win,use show to notshow
entry = tkinter.Entry(win,textvariable = e1,show = "*")

在这里插入图片描述

二、通过按钮获取输入内容

import tkinter

win = tkinter.Tk()

win.title("JJsPython")

win.geometry("300x300+250+150")

e = tkinter.Variable()
entry = tkinter.Entry(win,textvariable = e)
entry.pack()

def func():
    print(e.get())

button = tkinter.Button(win,text = "commit", command = func)
button.pack()

win.mainloop()

在这里插入图片描述

三、显示文本:

import tkinter

win = tkinter.Tk()

win.title("JJsPython")

win.geometry("300x300+250+150")

text = tkinter.Text(win,width = 100, height = 30)
text.pack()

str = """
Mother is the most important person to us. Now people celebrate Mother’s Day every year. It has become more and more popular. This day is on the second Sunday in May. On that day, children often send their mother flowers to express their love. And some children make small gifts for their mothers.
In last year's Mother's Day, I didn't spend it with my mother because she was in the hometown and I was at school, which made me feel ashamed. At night, I called my mother. At that time, I left my tears, and my mother told me that I should study hard at school. In the end, I said to her “Happy Mother’ Day, I love you" and she was very surprised and happy. 

"""

text.insert(tkinter.INSERT,str)

win.mainloop()

在这里插入图片描述

四、在显示文本的基础上加入滚动条吧

import tkinter

win = tkinter.Tk()
win.title("JJsPython")
win.geometry("300x300+250+150")

#create a scroll
scroll = tkinter.Scrollbar()
text = tkinter.Text(win,width=100,height=40)

#show scrollbar position
scroll.pack(side = tkinter.RIGHT,fill = tkinter.Y)
#text show
text.pack(side = tkinter.LEFT,fill = tkinter.Y)

#tide scrollbar and text
scroll.config(command = text.yview)
#tide text and scrollbar
text.config(yscrollcommand = scroll.set)

str = """
Mother is the most important person to us. Now people celebrate Mother’s Day every year. It has become more and more popular. This day is on the second Sunday in May. On that day, children often send their mother flowers to express their love. And some children make small gifts for their mothers.
In last year's Mother's Day, I didn't spend it with my mother because she was in the hometown and I was at school, which made me feel ashamed. At night, I called my mother. At that time, I left my tears, and my mother told me that I should study hard at school. In the end, I said to her “Happy Mother’ Day, I love you" and she was very surprised and happy. 
Mother is the most important person to us. Now people celebrate Mother’s Day every year. It has become more and more popular. This day is on the second Sunday in May. On that day, children often send their mother flowers to express their love. And some children make small gifts for their mothers.
In last year's Mother's Day, I didn't spend it with my mother because she was in the hometown and I was at school, which made me feel ashamed. At night, I called my mother. At that time, I left my tears, and my mother told me that I should study hard at school. In the end, I said to her “Happy Mother’ Day, I love you" and she was very surprised and happy. 
"""

text.insert(tkinter.INSERT,str)


win.mainloop()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值