我创建了一个循环来搜索CSV文件并返回带有指定关键字的行。标记else函数时,循环可以正常工作。
import csv
from tkinter import *
import tkinter.messagebox as box
master = Tk()
label1 = Label(master, text = 'User_ID', relief = 'groove', width = 40)
label2 = Label(master, text = 'User_Info', relief = 'groove', width = 40, height = 5)
e1 = Entry(master, relief = 'groove', width = 40)
e2 = Text(master, relief = 'groove', width = 40, height = 5, borderwidth = 2)
def enter():
csvfile = open('stack_example.csv', 'r')
read = csv.reader(csvfile)
for row in read:
if str(e1.get()) in row:
e2.insert("1.0", row, 'r')
#break
#else:
#box.showinfo('Search Result','Not Found')
#master.mainloop()
button3 = Button(master, text = 'Retrieve File', relief = 'groove', width = 25, command=enter)
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 2, column = 1, padx = 10 )
e1.grid( row = 1, column = 2, padx = 10 )
e2.grid( row = 2, column = 2, padx = 10 )
button3.grid( row = 3, column = 1, columnspan = 2)
但是,当else函数是代码的一部分时,循环将直接跳到else命令,而忽略if命令。上面产生的输出是将带有关键字的行从csv文件插入到文本小部件中。但是,下面的输出是一个显示框。 (使用相同的entry关键字,即在csv文件中)
import csv
from tkinter import *
import tkinter.messagebox as box
master = Tk()
label1 = Label(master, text = 'User_ID', relief = 'groove', width = 40)
label2 = Label(master, text = 'User_Info', relief = 'groove', width = 40, height = 5)
e1 = Entry(master, relief = 'groove', width = 40)
e2 = Text(master, relief = 'groove', width = 40, height = 5, borderwidth = 2)
def enter():
csvfile = open('stack_example.csv', 'r')
read = csv.reader(csvfile)
for row in read:
if str(e1.get()) in row:
e2.insert("1.0", row, 'r')
#break
else:
box.showinfo('Search Result','Not Found')
master.mainloop()
button3 = Button(master, text = 'Retrieve File', relief = 'groove', width = 25, command=enter)
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 2, column = 1, padx = 10 )
e1.grid( row = 1, column = 2, padx = 10 )
e2.grid( row = 2, column = 2, padx = 10 )
button3.grid( row = 3, column = 1, columnspan = 2)
我的理解是,仅当if语句为false时,else命令才会执行。但是,else语句似乎用此代码覆盖了if语句。我不确定如何编写一个可以起作用的循环:
答案 0 :(得分:0)
在<!DOCTYPE html>
<html ng-app='myApp'>
<head>
<script data-require="angular.js@4.0.0" data-semver="4.0.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>
<script data-require="angular.js@4.0.0" data-semver="4.0.0" src="script.ts"></script>
<script data-require="angular.js@4.0.0" data-semver="4.0.0" src="system.config.js"></script>
<script data-require="angular.js@4.0.0" data-semver="4.0.0" src="tsconfig.json"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller='myController'>
<button ng-mousedown="mouseDown()" ng-mouseup="mouseUp()">Mouse Down</button>
<br>
<span>Doing something {{times}} times</span>
</body>
</html>
循环中至少观察到一次any
条件时,文本将更改。因此,您的template<typename T> void visit(T &&t) { std::cout << "Hi " << t << "!\n"; }
void try_visit(std::any &&) { std::cout << "Unknown type\n"; }
template<typename T, typename... Ts> void try_visit(std::any thing) {
if(thing.type() == typeid(T)) {
visit(std::any_cast<T>(thing));
return;
}
if constexpr(sizeof...(Ts) > 0) try_visit<Ts...>(std::move(thing));
else try_visit(std::move(thing));
}
int main() {
try_visit<short, int, double, bool, long>(std::any{42});
}
语句代码可能仍在评估中,但是您还在同一循环中观察else
的行为。试试这个:
for