python设计图形界面_Python图形界面设计-Go语言中文社区

1、按钮对话框,并显示输入对应的内容

from tkinter import *

import tkinter.messagebox as messagebox

class Application(Frame):

def __init__(self, master=None):

Frame.__init__(self, master)

self.pack()

self.createWidgets()

def createWidgets(self):

self.nameInput = Entry(self)

self.nameInput.pack()

self.alertButton = Button(self, text='Hello', command=self.hello)

self.alertButton.pack()

def hello(self):

name = self.nameInput.get() or 'world'

messagebox.showinfo('Message', 'CT/TE, %s' % name)

app = Application()

# 设置窗口标题:

app.master.title('独爱空城梦')

# 主消息循环:

app.mainloop()

c8c84953027f603d50a7e28e32294c4a.png

2、文本框,显示相对应的文字

import tkinter as tk

app = tk.Tk()

app.title("FishC Demo")

#标签控件;可以显示文本和位图

theLable = tk.Label(app, text="你若安好,便是晴天",width=20,height=10)

theLable.pack()

app.mainloop()

e25ad8bec97b604f53a40c63698407e0.png

3、按钮与文本框,运行该代码时,点击按钮,在控制台会输出相对应的文字

import tkinter as tk

class App: #定义类

def __init__(self, master):

frame = tk.Frame(master)#定义窗体框架

frame.pack(side=tk.RIGHT, padx=100,pady=100)

#定义按钮

self.hi_there = tk.Button(frame, text="打招呼",bg="red" ,fg='green',command=self.say_hi)

self.hi_there.pack(padx=10, pady=10)#位置

def say_hi(self):

print("嗨,好久不见,你可安好!")

root = tk.Tk()

app = App(root)

root.mainloop()

1ad773b7511a3619818fc48504eedfdf.png

9991c5208b66c18047b8e1a2d6aefd4b.png

4、制作一个动态的五颜六色的树

from turtle import *

# 设置色彩模式是RGB:

colormode(255)

lt(90)

lv = 14

l = 120

s = 45

width(lv)

# 初始化RGB颜色:

r = 0

g = 0

b = 0

pencolor(r, g, b)

penup()

bk(l)

pendown()

fd(l)

def draw_tree(l, level):

global r, g, b

# save the current pen width

w = width()

# narrow the pen width

width(w * 3.0 / 4.0)

# set color:

r = r + 1

g = g + 2

b = b + 3

pencolor(r % 200, g % 200, b % 200)

l = 3.0 / 4.0 * l

lt(s)

fd(l)

if level < lv:

draw_tree(l, level + 1)

bk(l)

rt(2 * s)

fd(l)

if level < lv:

draw_tree(l, level + 1)

bk(l)

lt(s)

width(w)

speed("fastest")

draw_tree(l, 4)

done()

af739beba2632f7aee0525a35e1714bc.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Python Tkinter图形界面日历的实现方法: ```python import calendar import tkinter as tk from tkinter import ttk class Calendar: def __init__(self, parent, values): self.values = values self.parent = parent self.cal = calendar.TextCalendar(calendar.SUNDAY) self.year = 1 self.month = 1 self.wid = [] self.day_selected = 1 self.month_selected = self.month self.year_selected = self.year self.day_name = '' self.setup(self.year, self.month) def clear(self): for w in self.wid[:]: w.grid_forget() #w.destroy() self.wid.remove(w) def go_prev(self): if self.month > 1: self.month -= 1 else: self.month = 12 self.year -= 1 self.clear() self.setup(self.year, self.month) def go_next(self): if self.month < 12: self.month += 1 else: self.month = 1 self.year += 1 self.clear() self.setup(self.year, self.month) def selection(self, day, name): self.day_selected = day self.month_selected = self.month self.year_selected = self.year self.day_name = name self.values['day_selected'] = day self.values['month_selected'] = self.month self.values['year_selected'] = self.year self.values['day_name'] = name self.clear() self.setup(self.year, self.month) def setup(self, y, m): left = tk.Button(self.parent, text='<', command=self.go_prev) self.wid.append(left) left.grid(row=0, column=1) header = tk.Label(self.parent, height=2, text='{} {}'.format(calendar.month_abbr[m], str(y))) self.wid.append(header) header.grid(row=0, column=2, columnspan=3) right = tk.Button(self.parent, text='>', command=self.go_next) self.wid.append(right) right.grid(row=0, column=5) days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for num, name in enumerate(days): t = tk.Label(self.parent, text=name[:3]) self.wid.append(t) t.grid(row=1, column=num) for w, week in enumerate(self.cal.monthdayscalendar(y, m), 2): for d, day in enumerate(week): if day: #print(calendar.day_name[day]) b = tk.Button(self.parent, width=1, text=day, command=lambda day=day:self.selection(day, calendar.day_name[(day-1) % 7])) self.wid.append(b) b.grid(row=w, column=d) sel = tk.Label(self.parent, height=2, text='{} {} {} {}'.format( self.day_name, calendar.month_name[self.month_selected], self.day_selected, self.year_selected)) self.wid.append(sel) sel.grid(row=8, column=0, columnspan=7) ok = tk.Button(self.parent, width=5, text='OK', command=self.kill_and_save) self.wid.append(ok) ok.grid(row=9, column=2, columnspan=3, pady=10) def kill_and_save(self): self.parent.destroy() def main(): root = tk.Tk() root.title("Calendar") app = Calendar(root, {}) root.mainloop() if __name__ == '__main__': main() ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值