python3的tkinter 多程序迴圈

2021-08-26 05:30:27 字數 3082 閱讀 7518

tkinter python3 的多執行緒。
#!/use/bin/python

#base python3 code

#-*-coding:utf-8-*-

from tkinter import *

import threading, time

import tkinter.messagebox

# gui視窗類

class control:

# 定義gui介面

def __init__(self, master, fuc):

self.parent = master #tk的主線程

self.fuc = fuc #子執行緒函式

self.parent.title("title is here")#視窗標題

self.parent.geometry("300x400")#視窗大小

self.parent.resizable(0, 0) #不可變更視窗大小

# tkinter 控制項

self.label1 = label(self.parent, text="這是lable 標籤")

self.label1.grid(row=0, column=0)

#文字框

self.string1 = stringvar()

self.string1.set("這裡設定文字框內容")

self.textfiled1 = entry(self.parent, width =16, textvariable=self.string1)

self.textfiled1.grid(row=1, column=0)

#按鈕self.btn = button(self.parent, text="執行單次程式", command=self.button_fuc)

self.btn.grid(row=2, column=0)

self.btn = button(self.parent, text="點一下啟動乙個執行緒迴圈程式\n所以啟動後要禁用", command=self.fuc)

self.btn.grid(row=3, column=1)

#多行文字框

self.txinfo = text(self.parent, height=10, width=15)

self.txinfo.grid(row=3, column=0)

self.txinfo.insert(end,"這裡是多行\n內容")

#多選控制項

self.ynbox = checkbutton(self.parent, text="是否選擇...")

self.ynbox.grid(row=4,column = 0)

#組群方塊區(可以在此控制項上層增加控制項)

self.labelframe1 = labelframe(self.parent, width=25, height=30, text="組群方塊")

self.labelframe1.grid(row=0, column=1,rowspan=5, sticky=n) #跨行布局

self.lfynbox = checkbutton(self.labelframe1, text="是否選擇...")

self.lfynbox.grid(row=0,column = 0)

#定義停止按鈕的功能

def button_fuc(self):

#提示對話方塊

tkinter.messagebox.showinfo(message="這是訊息")

pass

#定義方法

def win_msg(self):

pass

#定義選項功能:

def select_fuc(self):

pass

# 具體功能類

class threadroot:

#初始化tk root

def __init__(self, master):

self.master = master

self.gui = control(self.master, self.starting)

# 以上將我們定義的gui類賦給服務類的屬性,將執行的功能函式作為引數傳入

# 定義執行緒1

def threading1(self):

while thread_state:

time.sleep(1) #間隔1s

print("無限迴圈,不卡機") #可以無限迴圈而不卡死主程序

# 為方法開第乙個單獨的執行緒

def starting(self):

#啟動執行緒1

self.thread1 = threading.thread(target=self.threading1)

self.thread1.setdaemon(true) #執行緒守護,即主程序結束後,此執行緒也結束。否則主程序結束子程序不結束

self.thread1.start()

##########################################

# #定義執行緒2 和

# def threading2(self):

# pass

#為方法開第二個單獨的執行緒(沒有測試過哈,,原理上control要傳遞第三個引數)

# def starting2(self):

# #啟動執行緒2

# self.thread1 = threading.thread(target=self.threading1)

# self.thread1.setdaemon(true) #執行緒守護,即主程序結束後,此執行緒也結束。否則主程序結束子程序不結束

# self.thread1.start()

##########################################

if __name__ == '__main__':

thread_state = false #子執行緒迴圈標識位(可選項)

root = tk()

tool = threadroot(root)

root.mainloop()

python3中,Tkinter編寫選單介面案例

python3中,tkinter編寫選單介面案例 from tkinter import top tk top.wm title 選單 top.geometry 400x300 300 100 建立乙個選單項,類似於導航欄 menubar menu top 建立選單項 fmenu1 menu top...

python3安裝 Python3的安裝

1.anaconda安裝 2.安裝包安裝 3.linux下的命令列安裝 centos red hat 1 sudo yum install y sudo yum update3 sudo yum install y python35u python35u libs python35u devel p...

python3的樣子 python3 基礎

第一周 語言基礎 一 python是一門什麼樣的語言?解釋型動態型別強型別定義語言。二 python2和3的主要區別?1 python3裡可以直接預設寫中文,然後python2裡是不能直接寫的。2 python2裡print不用加括號,可直接寫字串,也可以加 python3裡printi必須要加括號...