python中2个while循环_如何使while为真:在python中两个循环同时运行

基本上我需要同时运行两个while循环。原因是我需要一个循环来更新GUI,另一个循环来检查程序是否连接到internet。所以也许这需要多线程,或者我只需要一个网页就可以解决我的问题。以下是我的源代码:# -*- coding: utf-8 -*-

import sys

import urllib2

from Tkinter import *

import time

import socket

def TheMainProgram():

airid = 'http://www.aviationweather.gov/metar/data?ids={airid}&format=raw&hours=0&taf=off&layout=off&date=0'.format(airid=e1.get())

website = urllib2.urlopen(airid)

website_html = website.read()

data_start = website_html.find("")

br1 = data_start + 25

br2 = website_html.find("
")

metar_slice = website_html[br1:br2]

print("Here is the undecoded METAR data:\n"+metar_slice)

root2 = Tk()

root2.title("#Conection_Made#")

metar_dat_label = Label(root2, text="Here is the undecoded METAR data:")

metar_dat_label_ln_2 = Label(root2, text=metar_slice)

metar_dat_label.grid(row=0)

metar_dat_label_ln_2.grid(row=1)

def _quit():

sys.exit()

def internet(host="8.8.8.8", port=53, timeout=3):

try:

socket.setdefaulttimeout(timeout)

socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))

return True

except Exception:

return False

def tkupdate():

while True:

root1.update()

def internet_loop():

while True:

out = internet()

if out == True:

connetion2internet.configure(text="YOU ARE CONNECTED TO THE INTERNET", fg="green")

if out == False:

connetion2internet.configure(text="YOU ARE NOT CONNECTED TO THE INTERNET", fg="red")

root1 = Tk()

root1.title("Welcome")

warning = Label(root1, text="*********** WARNING REQURIES INTERET TO RUN ***********")

warning.grid(row=0)

connetion2internet = Label(root1, text="")

connetion2internet.grid(row=1)

airport_code = Label(root1,text="Enter Airport Code:")

airport_code.grid(row=2, sticky="W")

e1 = Entry(root1)

e1.grid(row=2, sticky="E")

button1 = Button(root1, text="Contunue", command=daMainProgram).grid(row=3, sticky="W")

button2 = Button(root1, text="Quit", command=_quit)

button2.grid(row=3, sticky="E")

tkupdate()

internet_loop()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值