如何在我的文本中找到特定文本字符串的位置?
例如,我有文本“Lorem ipsum dolor sat amet, consectetur adipiscing elit.”,当我搜索“ipsum”时,我想找出它的位置。
if "ipsum" in text:
print position
else:
pass
Run Code Online (Sandbox Code Playgroud)
我正在寻找的位置类似于“1.6”如果我想更改文本的颜色,它看起来像这样:
text.tag_add("foo", "1.28", "1.39")
Run Code Online (Sandbox Code Playgroud)
然后为了改变我搜索过的文本的颜色,我需要以某种方式获得它的位置,对吗?我可能是错的,但我正在寻找一些示例或指导。谢谢!:)
文件移动基于上次更新.仅使用按钮3,我在目录末尾缺少斜杠.这会导致路径与我尝试移动的文本文件合并:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
return self.func(*args)
File "C:/Python34/check2.py", line 30, in fileMove
if os.stat(src).st_mtime > now - 1 * 86400:
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:/Somewhere/filenametextfile'
Run Code Online (Sandbox Code Playgroud)
import shutil, sys, time, os
import tkinter as tk
from tkinter import *
import tkinter.filedialog
root = Tk()
def fileMove():
sourcePath = filedialog.askdirectory()
receivePath = filedialog.askdirectory()
source = sourcePath
dest = receivePath
files = os.listdir(source)
now = …Run Code Online (Sandbox Code Playgroud) from tkinter import *
from tkinter import messagebox
root= Tk()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
错误:
...回溯(最近一次调用最后一次):文件“C:\Users\Lee\Desktop\Python items\tkinter.py”,第 1 行,导入 tkinter as Tk 文件“C:\Users\Lee\Desktop\Python” items\tkinter.py”,第 2 行,来自 tkinter import messagebox ImportError:无法导入名称“messagebox”
[25.248秒内完成]我不知道为什么messagebox不会导入?
我需要从指向黄色框的蓝色和绿色框绘制两个箭头。我尝试使用 create_line 绘制对角线,但没有用。任何人都可以建议我可以绘制这些箭头的任何方法。使用 create_line 时的错误消息是:AttributeError: '_tkinter.tkapp' object has no attribute 'create_line'
from tkinter import *
import tkinter as tk
window = Tk()
window.geometry("900x500")
window.configure(background='red')
window.title("Theoretical")
label1 = Label(window, text="Hess' cycle of combustion", fg="black", bg="red", font=("Comic Sans MS", 20))
label1.pack()
text1 = Text(window, width=20, height = 1, bg= "blue")
text1.place(x=200, y=100)
window.create_line(0, 0, 200, 100)
window.create_line(0, 100, 200, 0, fill="white")
text2 = Text(window, width=20, height = 1, bg= "green")
text2.place(x=520, y=100)
text3 = Text(window, width=20, height = 1, bg= "yellow")
text3.place(x=370, …Run Code Online (Sandbox Code Playgroud) 问题:
我有一个 for 循环,它将文本添加到文本框。我想要一个按钮位于文本之后、每次迭代在文本框中结束之前的最右侧。
另外,是否可以将某种标签(即其之前的文本)分配给按钮?因此,对于每次迭代,按钮都会调用一个函数并在其之前传递文本。
预期图像:
我是python编程的初学者,必须做一个必须在学校提交的项目。我在一个月前发现了tkinter和ttk,并且想知道哪种代码更好。
我程序的目的是使其与用户交互,并使其看起来尽可能现代。如果您能告诉我tkinter相对ttk的优势,反之亦然,以及我建议您使用哪一种,我将不胜感激。
该计划的目的是与某个地点的居民进行互动,并允许他访问该地点的不同功能以及类似的东西。
#snakes and ladder
from tkinter import * #pygame is the module that has collections of functions that is used to create a window import everything from tkinter
import time
class window(Frame): #Frame comes from tkinter is what you think as a window
def __init__(self, master = None):#The master widget defines the settings upon initialisation
Frame.__init__(self, master) #This is called the frame class that has been built in python
self.master = master
def __init__window(self): #creation of the init window
self.master.title("Reagan Kambayi") …Run Code Online (Sandbox Code Playgroud) 因此,您知道例如在Windows上使用记事本时想要打开旧文件的方式吗?您单击文件,然后打开,然后将打开一个文件对话框,您可以选择所需的文件,程序将显示其内容。
基本上,我想在Python中创建一个可以完成此操作的按钮。
这是我按钮的功能-
def UploadAction():
#What to do when the Upload button is pressed
from tkinter import filedialog
Run Code Online (Sandbox Code Playgroud)
当我单击分配给该操作的按钮时,什么也没有发生,没有错误,没有崩溃,只是什么也没有。
当分数大于或等于 5 时,我试图打印出一个字符串,但它不会打印。我在这里做错了什么?
from tkinter import *
top = Tk()
top.geometry("1000x720")
Score = 0
var = StringVar()
sline = Label(top, textvariable=var)
sline.place(x = 100, y = 395)
var.set("Score: ")
scorevar = StringVar()
label = Label(top, textvariable=scorevar)
label.place(x = 140, y = 395)
scorevar.set("0")
def getscore():
global Score
Score = Score + 1
scorevar.set(Score)
print(Score)
bclick = Button(top, fg='white', bg='RoyalBlue3', activebackground='azure', highlightcolor='azure', bd=4, padx=10, pady=5, text = "Click!", command = getscore)
bclick.place(x = 100,y = 350)
if Score >= 5:
print("Hey, …Run Code Online (Sandbox Code Playgroud) tkinter ×10
python ×8
python-3.x ×4
if-statement ×1
import ×1
python-2.7 ×1
taskdialog ×1
tk-toolkit ×1
ttk ×1