導航:首頁 > 編程語言 > python中tkinter教程

python中tkinter教程

發布時間:2022-07-18 02:13:52

『壹』 python里tkinter如何重置單選按鈕

打開圖像時,使用單選按鈕注釋值。在
在列表中收集此值

因此,在這個例子中,我有2個復合詞,列表將有2個注釋。在

在import Tkinter as tk

from PIL import ImageTk, Image

from tkFileDialog import askopenfilename

cmp_list = ["VU435DR","VU684DR"]

li = []

li_final = []

def sel():

selection = str(var.get())

if selection == "1":

li.append("Antagonist")

elif selection == "2":

li.append("Agonist")

for i in range(len(cmp_list)):

root = tk.Tk()

var = tk.IntVar()

ig = str(cmp_list[i] + '.png')

img = ImageTk.PhotoImage(Image.open(ig))

panel = tk.Label(root,image=img)

panel.pack(side = "top",fill="none",expand="no")

#w = tk.Text(height=2,width=50)

#w.pack(side='right")

q = tk.Radiobutton(root,text="Antagonist",command=sel,value=1,variable=var)

q.pack()

r = tk.Radiobutton(root,text="Agonist",command=sel,value=2,variable=var)

r.pack()

root.mainloop()

print li

『貳』 Python 中用 Tkinter GUI編程

可以使用sqlite,下面是使用方法。

  1. 導入PythonSQLITE資料庫模塊

Python2.5之後,內置了SQLite3,成為了內置模塊,這給我們省了安裝的功夫,只需導入即可~

importsqlite3


2.創建/打開資料庫


在調用connect函數的時候,指定庫名稱,如果指定的資料庫存在就直接打開這個資料庫,如果不存在就新創建一個再打開。


cx=sqlite3.connect("E:/test.db")

也可以創建資料庫在內存中。

con=sqlite3.connect(":memory:")

3.資料庫連接對象


打開資料庫時返回的對象cx就是一個資料庫連接對象,它可以有以下操作:


commit()--事務提交


rollback()--事務回滾

close()--關閉一個資料庫連接

cursor()--創建一個游標


關於commit(),如果isolation_level隔離級別默認,那麼每次對資料庫的操作,都需要使用該命令,你也可以設置isolation_level=None,這樣就變為自動提交模式。

4.使用游標查詢資料庫


我們需要使用游標對象SQL語句查詢資料庫,獲得查詢對象。通過以下方法來定義一個游標。


cu=cx.cursor()

游標對象有以下的操作:

execute()--執行sql語句

executemany--執行多條sql語句

close()--關閉游標

fetchone()--從結果中取一條記錄,並將游標指向下一條記錄

fetchmany()--從結果中取多條記錄

fetchall()--從結果中取出所有記錄

scroll()--游標滾動


1.建表

cu.execute("createtablecatalog(idintegerprimarykey,pidinteger,namevarchar(10)UNIQUE,nicknametextNULL)")


上面語句創建了一個叫catalog的表,它有一個主鍵id,一個pid,和一個name,name是不可以重復的,以及一個nickname默認為NULL。


2.插入數據


請注意避免以下寫法:


#Neverdothis--insecure會導致注入攻擊


pid=200

c.execute("...wherepid='%s'"%pid)

正確的做法如下,如果t只是單個數值,也要採用t=(n,)的形式,因為元組是不可變的。

fortin[(0,10,'abc','Yu'),(1,20,'cba','Xu')]:

cx.execute("insertintocatalogvalues(?,?,?,?)",t)

簡單的插入兩行數據,不過需要提醒的是,只有提交了之後,才能生效.我們使用資料庫連接對象cx來進行提交commit和回滾rollback操作.

cx.commit()


3.查詢

cu.execute("select*fromcatalog")


要提取查詢到的數據,使用游標的fetch函數,如:


In[10]:cu.fetchall()

Out[10]:[(0,10,u'abc',u'Yu'),(1,20,u'cba',u'Xu')]

如果我們使用cu.fetchone(),則首先返回列表中的第一項,再次使用,則返回第二項,依次下去.


4.修改

In[12]:cu.execute("updatecatalogsetname='Boy'whereid=0")

In[13]:cx.commit()

注意,修改數據以後提交


5.刪除


cu.execute("deletefromcatalogwhereid=1")

cx.commit()


6.使用中文

請先確定你的IDE或者系統默認編碼是utf-8,並且在中文前加上u


x=u'魚'

cu.execute("updatecatalogsetname=?whereid=0",x)

cu.execute("select*fromcatalog")

cu.fetchall()

[(0,10,u'u9c7c',u'Yu'),(1,20,u'cba',u'Xu')]

如果要顯示出中文字體,那需要依次列印出每個字元串


In[26]:foritemincu.fetchall():

....:forelementinitem:

....:printelement,

....:print

....:

010魚Yu

120cbaXu


7.Row類型

Row提供了基於索引和基於名字大小寫敏感的方式來訪問列而幾乎沒有內存開銷。原文如下:

sqlite3.Rowprovidesbothindex-basedandcase-insensitivename-.-basedapproachorevenadb_rowbasedsolution.

Row對象的詳細介紹


classsqlite3.Row

_factoryforConnectionobjects..


,iteration,representation,equalitytestingandlen().


,theycompareequal.


Changedinversion2.6:Addediterationandequality(hashability).


keys()

.Immediatelyafteraquery,.description.


Newinversion2.6.


下面舉例說明


In[30]:cx.row_factory=sqlite3.Row


In[31]:c=cx.cursor()


In[32]:c.execute('select*fromcatalog')

Out[32]:<sqlite3.Cursorobjectat0x05666680>


In[33]:r=c.fetchone()


In[34]:type(r)

Out[34]:<type'sqlite3.Row'>


In[35]:r

Out[35]:<sqlite3.Rowobjectat0x05348980>


In[36]:printr

(0,10,u'u9c7c',u'Yu')


In[37]:len(r)

Out[37]:4


In[39]:r[2]#使用索引查詢

Out[39]:u'u9c7c'


In[41]:r.keys()

Out[41]:['id','pid','name','nickname']


In[42]:foreinr:

....:printe,

....:

010魚Yu


使用列的關鍵詞查詢

In[43]:r['id']

Out[43]:0


In[44]:r['name']

Out[44]:u'u9c7c'

『叄』 python圖形化界面設計tkinter

python提供了多個圖形開發界面的庫,幾個常用Python GUI庫如下:

『肆』 怎麼在Python嵌入版中使用Tkinter

方法/步驟

『伍』 python tkinter 如何獲取文本框中的內容

1、首先打開python,輸入:
#coding:utf-8
importurllib,urllib2
importTkinter#導入TKinter模塊
2、然後輸度入:
ytm=Tkinter.Tk()#知創建Tk對象
ytm.title("login")#設置窗口標題
ytm.geometry("300x300")#設置窗口尺寸
3、然後輸入:
l1=Tkinter.Label(ytm,text="用戶名")#標簽道
l1.pack()#指定包管理器放置組件
user_專text=Tkinter.Entry()#創建文本框
4、然後輸入:
user_text.pack()
defgetuser():
user=user_text.get()#獲取文本框內容
printuserTkinter.Button(ytm,text="登錄",command=getuser).pack()#command綁定屬獲取文本框內容方法
ytm.mainloop()#進入主循環,就完成了。

『陸』 怎麼在Python嵌入版使用Tkinter

額,上網下載TKinter.
然後按winows+r打開運行,輸入cmd然後在cmd窗口輸入cd <path>
這里的path是指你下載tkinter包後解壓的文件路徑,不用帶括弧。
輸入完後輸入pip TKinter install等待
如出現TKinter........install successful便安裝成功。打開IDLE輸入import tkinter便可以使用

『柒』 python怎麼用tkinter

Tkinter 是使用 python 進行窗口視窗設計的模塊。Tkinter模塊("Tk 介面")是Python的標准Tk GUI工具包的介面。作為 python 特定的GUI界面,是一個圖像的窗口,tkinter是python 自帶的,可以編輯的GUI界面,我們可以用GUI 實現很多直觀的功能,比如想開發一個計算器,如果只是一個程序輸入,輸出窗口的話,是沒用用戶體驗的。所有開發一個圖像化的小窗口,就是必要的。
對於稍有GUI編程經驗的人來說,Python的Tkinter界面庫是非常簡單的。python的GUI庫非常多,選擇Tkinter,一是最為簡單,二是自帶庫,不需下載安裝,隨時使用,三則是從需求出發,Python作為一種腳本語言,一種膠水語言,一般不會用它來開發復雜的桌面應用,它並不具備這方面的優勢,使用Python,可以把它作為一個靈活的工具,而不是作為主要開發語言,那麼在工作中,需要製作一個小工具,肯定是需要有界面的,不僅自己用,也能分享別人使用,在這種需求下,Tkinter是足夠勝任的!

『捌』 python用tkinter創建一個登錄界面

import tkinter
import tkinter.ttk
import tkinter.messagebox

root = tkinter.Tk()
root.title("登錄")
userlabel = tkinter.Label(root,text="用戶名:")
passwordlabel = tkinter.Label(root,text="密碼:")
userlabel.grid(row=0,column=0)
passwordlabel.grid(row=1,column=0)
userentry = tkinter.ttk.Entry(root)
passwordentry = tkinter.ttk.Entry(root,show="●")
userentry.grid(row=0,column=1,padx=10,pady=2)
passwordentry.grid(row=1,column=1,padx=10,pady=2)

def get():
tkinter.messagebox.showinfo("結果","用戶名:" + userentry.get() + " 密碼:" + passwordentry.get())

yes = tkinter.ttk.Button(root,text="確定",command=get)
yes.grid(row=2,column=1)
root.mainloop()

效果圖

『玖』 python中tkinter模塊如何消毀組件

如何在tkinter窗體上動態創建組件以及銷毀組件的方法。

import tkinter

import tkinter.messagebox

import tkinter.simpledialog

btnList = []

# 動態創建組件,並計算組件在窗體上的位置

def place(n):

for i in range(n):

exec('btn'+str(i)+'=tkinter.Button(root,text='+str(i)+')')

eval('btn'+str(i)).place(x=80, y=10+i*30, width=60, height=20)

btnList.append(eval('btn'+str(i)))

root.geometry('200x'+str((n)*30+70)+'+400+300')

return n*30 + 10

# 創建tkinter應用程序

root = tkinter.Tk()

# 窗口標題

root.title('動態創建組件')

# 窗口初始大小和位置

root.geometry('200x180+400+300')

# 不允許改變窗口大小

root.resizable(False, False)

# 增加按鈕的按鈕

def btnSetClick():

n = tkinter.simpledialog.askinteger(title='輸入一個整數',

prompt='想動態增加幾個按鈕:',

initialvalue=3)

if n and n>0:

startY = place(n)

modify(startY)

# 根據需要禁用和啟用「增加按鈕」和「清空按鈕」

btnSet['state'] = 'disabled'

btnClear['state'] = 'normal'

btnSet = tkinter.Button(root,

text='增加按鈕',

command=btnSetClick)

def btnClearClick():

global btnList

# 刪除動態創建的按鈕

for btn in btnList:

btn.destroy()

btnList = []

modify(20)

btnClear['state'] = 'disabled'

btnSet['state'] = 'normal'

btnClear = tkinter.Button(root,

text='清空按鈕',

command=btnClearClick)

# 默認處於禁用狀態

btnClear['state'] = 'disabled'

# 動態調整「增加按鈕」和「清空按鈕」的位置

def modify(startY):

btnSet.place(x=10, y=startY, width=80, height=20)

btnClear.place(x=100, y=startY, width=80, height=20)

modify(20)

root.mainloop()

代碼運行後初始狀態為:單擊「增加按鈕」後,在彈出的窗口中輸入5,然後窗體變為下面的樣子:

單擊「清空按鈕「後恢復到初始狀態。

閱讀全文

與python中tkinter教程相關的資料

熱點內容
狼狽電影百度雲 瀏覽:474
dos命令如何跳過程序 瀏覽:619
編譯器沒有游標怎麼辦 瀏覽:871
在天貓app怎麼看天貓積分 瀏覽:224
安卓手機怎麼下載gba電玩之家 瀏覽:770
周三多管理學pdf 瀏覽:460
一部美國電影講的是幾個小孩子 瀏覽:412
用手機怎麼把圖片放到一個文件夾 瀏覽:589
吃奶的電影照片 瀏覽:358
理論電影 台灣 瀏覽:757
日本打真軍電影 瀏覽:389
單片機全閃全亮 瀏覽:986
攻略禁忌肉文 瀏覽:397
安卓大屏導航怎麼看是什麼牌子的 瀏覽:897
美國動畫片主角是一個女孩 瀏覽:466
會員免費觀看的網站是什麼 瀏覽:761
無水印無版權的無需會員電影電視劇網站 瀏覽:925
北京靠譜程序員網 瀏覽:861
寬頻無法連接到伺服器是怎麼回事 瀏覽:47
去呼呼客棧管家系統源碼 瀏覽:637