python交互式圖形編程實(shí)例(一)
本文實(shí)例為大家分享了python交互式圖形編程的具體代碼,供大家參考,具體內(nèi)容如下
#!/usr/bin/env python3# -*- coding: utf-8 -*-
#溫度轉(zhuǎn)換
from graphics import *
win = GraphWin("攝氏溫度轉(zhuǎn)換器", 400, 300)
win.setCoords(0.0, 0.0, 3.0, 4.0)
# 繪制接口
Text(Point(1,3), " 攝氏溫度:").draw(win)
Text(Point(1,1), " 華氏溫度:").draw(win)
input = Entry(Point(2,3), 5)
input.setText("0.0")
input.draw(win)
output = Text(Point(2,1),"")
output.draw(win)
button = Text(Point(1.5,2.0),"轉(zhuǎn)換")
button.draw(win)
Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)
# 等待鼠標(biāo)點(diǎn)擊
win.getMouse()
# 轉(zhuǎn)換輸入
celsius = eval(input.getText())
fahrenheit = 9.0/5.0 * celsius + 32.0
# 顯示輸出,改變按鈕
output.setText(fahrenheit)
button.setText("退出")
# 等待響應(yīng)鼠標(biāo)點(diǎn)擊,退出程序
win.getMouse()
win.close()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#方塊移動(dòng)
from tkinter import *
def main():
tk = Tk()
canvas = Canvas(tk, width = 400, height = 400)
canvas.pack()
def moverectangle(event):
if event.keysym == "Up":
canvas.move(1,0,-5)
elif event.keysym == "Down":
canvas.move(1,0,5)
elif event.keysym == "Left":
canvas.move(1,-5,0)
elif event.keysym == "Right":
canvas.move(1,5,0)
canvas.create_rectangle(180,180,220,220,fill="red")
canvas.bind_all("<KeyPress-Up>",moverectangle)
canvas.bind_all("<KeyPress-Down>",moverectangle)
canvas.bind_all("<KeyPress-Left>",moverectangle)
canvas.bind_all("<KeyPress-Right>",moverectangle)
tk.mainloop()
if __name__ == '__main__':
main()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from graphics import *
def convert(input):
celsius = eval(input.getText()) # 輸入轉(zhuǎn)換
fahrenheit = 9.0/5.0 * celsius + 32
return fahrenheit
def colorChange(win,input):
cnum = eval(input.getText())
weight = cnum / 100.0
newcolor = color_rgb(int(255*weight),int(66+150*(1-weight)),int(255*(1-weight)))
win.setBackground(newcolor)
def main():
win = GraphWin("攝氏溫度轉(zhuǎn)換", 400, 300)
win.setCoords(0.0, 0.0, 3.0, 4.0)
# 繪制輸入接口
Text(Point(1,3),
" 攝氏溫度:").draw(win)
Text(Point(2,2.7),
" (請(qǐng)輸入: 0.0-100.0 )").draw(win)
Text(Point(1,1),
"華氏溫度:").draw(win)
input = Entry(Point(2,3), 5)
input.setText("0.0")
input.draw(win)
output = Text(Point(2,1),"")
output.draw(win)
button = Text(Point(1.5,2.0),"轉(zhuǎn)換")
button.draw(win)
rect = Rectangle(Point(1,1.5), Point(2,2.5))
rect.draw(win)
# 等待鼠標(biāo)點(diǎn)擊
win.getMouse()
result = convert(input) # 轉(zhuǎn)換輸入
output.setText(result) # 顯示輸出
# 改變顏色
colorChange(win,input)
# 改變按鈕字體
button.setText("退出")
# 等待點(diǎn)擊事件,退出程序
win.getMouse()
win.close()
if __name__ == '__main__':
main()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python selenium 查找隱藏元素 自動(dòng)播放視頻功能
這篇文章主要介紹了python selenium 查找隱藏元素 自動(dòng)播放視頻功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
用Python爬取618當(dāng)天某東熱門(mén)商品銷(xiāo)量數(shù)據(jù),看看大家喜歡什么!
618購(gòu)物節(jié),準(zhǔn)備分析一波購(gòu)物節(jié)大家都喜歡買(mǎi)什么?本文以某東為例,Python爬取618活動(dòng)的暢銷(xiāo)商品數(shù)據(jù),并進(jìn)行數(shù)據(jù)清洗,最后以可視化的方式從不同角度去了解暢銷(xiāo)商品中,名列前茅的商品是哪些?銷(xiāo)售數(shù)據(jù)如何?用戶好評(píng)如何?等等,需要的朋友可以參考下2021-06-06
淺析Python的web.py框架中url的設(shè)定方法
web.py是Python的一個(gè)輕量級(jí)Web開(kāi)發(fā)框架,這里我們來(lái)淺析Python的web.py框架中url的設(shè)定方法,需要的朋友可以參考下2016-07-07
python標(biāo)準(zhǔn)庫(kù)OS模塊詳解
這篇文章主要介紹了python標(biāo)準(zhǔn)庫(kù)OS模塊詳細(xì)介紹,需要的朋友可以參考下2020-03-03
python使用xpath中遇到:<Element a at 0x39a9a80>到底是什么?
這篇文章主要給大家詳細(xì)介紹了關(guān)于python使用xpath中遇到:<Element a at 0x39a9a80>的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
win10下python3.5.2和tensorflow安裝環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了win10下python3.5.2和tensorflow安裝環(huán)境搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
python 實(shí)現(xiàn)矩陣上下/左右翻轉(zhuǎn),轉(zhuǎn)置的示例
今天小編就為大家分享一篇python 實(shí)現(xiàn)矩陣上下/左右翻轉(zhuǎn),轉(zhuǎn)置的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01

