# -*- coding: utf-8 -*-
#导入名称与python2有一定变化
import tkinter
import tkinter.messagebox
import random
import time
from tkinter.filedialog import *
class Application(Frame):
#Frame所有Widget的父容器
def __init__(self, master=None):
Frame.__init__(self, master)
#如果子类的构造方法中没有显示的调用基类构造方法
#则系统默认调用基类无参数的构造方法
#而基类中又没有默认无参的构造方法,则编译出错。
#而且子类的构造函数,可能与父类的参数个数、顺序都不一致
#无法推测出应该如何调用父类的构造函数。所以需要手动调用父类构造函数。
#self.master=master
#master.geometry('888x666')
self.pack()
#pack()方法把Widget加入到父容器中,并实现布局。pack()是最简单的布局.
self.createWidgets()
self.fh2="null"
#fh2为保存读取后的总数据,为后面随机选择作准备
def createWidgets(self):
self.name = StringVar()
self.name.set("选择文件")
self.helloLabel = Label(self, textvariable=self.name, font = ("Arial, 30"), width=10, heigh=3)
self.helloLabel.pack()
self.alertButton = Button(self, text='Open', command=self.inputnumber, width=12, heigh=2)
self.alertButton.pack(side=LEFT)
#定义停靠在父组件的哪一边上,“top”, “bottom”, “left”, “right”,(默认为”top”)
self.startButton = Button(self, text='Start', command=self.randomStart, width=12, heigh=2)
self.startButton.pack(side=LEFT)
self.endButton = Button(self, text='End', command=self.randomStop, width=12, heigh=2)
self.endButton.pack(anchor="w")
#对齐方式,左对齐”w”,右对齐”e”,顶对齐”n”,底对齐”s”
def inputnumber(self):
fd = LoadFileDialog(self)
filename = fd.go()
# 显示打开文件对话框,并获取选择的文件名称
with open(filename,'r') as f:
self.fh2=f.readlines()
self.name.set("点击开始")
def randomStart(self):
self.n=1
#n用于点击stop时跳出循环
while self.n==1:
if self.n==0:
break
#num = random.randint(12, 20)
num = random.choice(self.fh2).replace('\n', '')
#替换掉多余换行
#time.sleep(0.1)
self.update()
#print(num)
self.name.set(num)
def randomStop(self):
self.n=0
#print(self.fh2)
app = Application()
# 设置窗口标题:
app.master.title('Random')
# 主消息循环:
app.mainloop()
#inconsistent use of tabs and spaces in indentation
#这个报错就是混用了tab和4个空格造成的
#检查代码,要不全部用tab,要不全部用4个空格
#或者用idle编辑器校正
python3 随机读取文件中某一行
猜你喜欢
转载自blog.csdn.net/u012700515/article/details/80068185
今日推荐
周排行